Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
copx <in*****@invalid.com> spoke thus:
Is realloc(p, 0) a valid operation or does it cause undefined behaviour?
It is valid.
http://www.eskimo.com/~scs/C-faq/q7.30.html
Can you still free(p) after this?
No.
That depends on how you invoke it.
If you just call
realloc(p, 0);
and ignore the returned value, p will be a pointer to deallocated
memory, and free(p) (or any ues of p) will invoke undefined behavior.
If you call it like this:
p = realloc(p, 0);
then the value of p will be either a null pointer or a valid pointer;
in either case, you can safely call free(p).
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.