You are not logged in.
How do I check if the memory location specified in the pointer is freed and available or not? in C.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
As far as I know, you can't. You are responsible for keeping track of allocated memory yourself.
What are you really trying to do?
Last edited by drcouzelis (2013-01-26 04:16:33)
Offline
What do you mean available? Can you give an example?
A memory address is just an address - it is up to you to keep track of whether space has been allocated at that location and what data may be stored there.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
As far as I know, you can't. You are responsible for keeping track of allocated memory yourself.
What are you really trying to do?
I want to see if the memory address is really available for other use after calling "free()".
It's just an experiment.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
You might try mprobe() or mcheck() but that only tells you if the memory pointed to is available right at that moment within the context of the thread. Another thread may allocate the memory before you do anything so have to be careful. And you cannot rely on a positive either, the memory may be reallocated to something else and being used by some different code, cannot assume its still being used for the same purpose.
Offline
No, it is most definitely not available for use after you free it. You should only free it when you no longer want to use it.
Unless, of course, you mean you wish to use the same pointer for a new (m/c)alloc. But in this case that function will return a *new* memory address.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Got it, thanks alot.
milo64.
Registered Linux User: #555436
My Blog @ http://milo64.blogspot.com/
Offline
You might try mprobe() or mcheck() but that only tells you if the memory pointed to is available right at that moment within the context of the thread. Another thread may allocate the memory before you do anything so have to be careful. And you cannot rely on a positive either, the memory may be reallocated to something else and being used by some different code, cannot assume its still being used for the same purpose.
You might also try to wrap it into an exception - if use is granted its OK, otherwise it spits out an exception. Not sure though.
---sometimes the obvious isn't there!---
Offline
Furthermore, you can't check if a pointer has been freed or not, as the value of the pointer is unchanged. I find it's often useful to have a "checked free" macro like this:
#define xfree(ptr) if(ptr != NULL) { free(ptr); ptr = NULL; }
Now you're safe from accidental double-frees (although, their presence indicates something is wrong with your logic somewhere, even if they're harmless) and you can easily see if a pointer has been freed (which, again, should really not be in doubt, but it's nice to be sure).
Last edited by Barrucadu (2013-02-07 11:47:36)
Offline
You might also try to wrap it into an exception - if use is granted its OK, otherwise it spits out an exception. Not sure though.
Are you using a different C than I'm using? Because my C doesn't have exception handling.
Offline
Are you using a different C than I'm using? Because my C doesn't have exception handling.
It kind of does: https://en.wikipedia.org/wiki/Exception … g_syntax#C
'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard
Offline
Oh dear, that is hideous. In any case, that is showing how to *implement* exception handling in C. There is no exception handling for functions like free - in the example linked one would have to still wrap the free function in something like Barracuda's example. Once that has been done, I see no point in the -jmp handling.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Oh dear, that is hideous.
Hence 'kind of'.
'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard
Offline
I suppose handling a signal like SEGV then longjmp'ing to a diagnosis routine for memory allocation is possible. If you were prototyping some code you might do that to figure something out. But that would be like Plan Z, lol. Returning to the start of this discussion, Plan A might be imagined as something like having a library of routines on top of malloc/free to which you could add diagnostic functions i.e. "is this pointer still being used or not?".
Offline
Furthermore, you can't check if a pointer has been freed or not, as the value of the pointer is unchanged. I find it's often useful to have a "checked free" macro like this:
#define xfree(ptr) if(ptr != NULL) { free(ptr); ptr = NULL; }
Now you're safe from accidental double-frees (although, their presence indicates something is wrong with your logic somewhere, even if they're harmless) and you can easily see if a pointer has been freed (which, again, should really not be in doubt, but it's nice to be sure).
free does nothing if the pointer is already NULL, so the if statement is pointless. Use this:
#define xfree(ptr) do { free(ptr); ptr = NULL; } while (0);
Offline