Khookie said:
Hi all
I was wondering whether there was a way to know whether a pointer (in
my case, I'm using a char pointer) has been allocated memory through
malloc or not, thus needing to be freed at a later stage?
Um, yeah, kinda. Ish. Well, no. But yes.
My! Wasn't that helpful! :-)
C doesn't support you in your bid to know this, but it doesn't forbid you
from knowing, either. When you allocate memory via malloc, calloc, or
realloc, you know that you are doing so (*but* see below). It is perfectly
possible to store a copy of the pointer thus obtained in some kind of
searchable list, for later lookup.
It is, however, a messy way to deal with memory deallocation, and you have
to remember to remove the copy from the lookup table when you free the
memory.
Also, if you call third-party libraries that allocate memory, well, they
won't know about your super new list, and so they won't know to update it
with the memory they allocate.
So, really, the answer is "no" unless you're prepared to do a *lot* of
work.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999