does anyone know a possibility (MS function or macro) tho check wether a
pointer is valid or not at runtime, i.e. still points to a block of
memory actually allocated with malloc? (To check NULL is not a solution,
because the pointer may become invalid by another function which freed
the memory)
For example:
int *ptr;
ptr = (int *) malloc(100 * sizeof(int));
_validatePtr(ptr); // OK rtn = true
free(ptr);
_validatePtr(ptr); // NOT OK rtn = false
Thanks in advance
Tom
Take a look at the IsBadReadPtr() / IsBadWritePtr() / IsBadStringPtr()
family of functions.
Regards,
Mike
"Tom Scherer" <tk.sc...@t-online.de> wrote in message
news:3D679009...@t-online.de...
If this is for verification or debugging purposes,
the IsBad{Read,Write,String}Ptr() functions may
help, or they may not, but you absolutely should
not attempt to use them to "fix" problems with your
application where you have lost control of its
adherance to the "one free() per malloc()" rule.
You may want to investigate tools such as Purify
which add a layer of checking over the memory
allocation functions to ferret out bugs such as
I suspect motivate your question. The only way
to fix such bugs is to design the code so that
it does adhere to the rule. There is no magic
solution such as you seem to seek with your post.
--
-Larry Brasfield
(address munged, s/sn/h/ to reply)