reallocptr 0 is not equivalent to freeptr
suggest changerealloc is conceptually equivalent to malloc + memcpy + free on the other pointer.
If the size of the space requested is zero, the behavior of realloc is implementation-defined. This is similar for all memory allocation functions that receive a size parameter of value 0. Such functions may in fact return a non-null pointer, but that must never be dereferenced.
Thus, realloc(ptr,0) is not equivalent to free(ptr). It may
- be a “lazy” implementation and just return
ptr free(ptr), allocate a dummy element and return thatfree(ptr)and return0- just return
0for failure and do nothing else.
So in particular the latter two cases are indistinguishable by application code.
This means realloc(ptr,0) may not really free/deallocate the memory, and thus it should never be used as a replacement for free.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents