The C90 standard specifically said:
If size is zero and ptr is not a null pointer, the object it points to is freed.
So this is how C90 and C++ implementations should behave - nice, simple, and straightforward.
However, for some reason that sentence was removed from the C99 standard.
POSIX currently has the sentence that's in the C90 standard. But, The Austin Group (which handles some aspects of the POSIX standardization) has a defect open on this. Note that the defect report indicates that BSD has the following behavior for realloc(ptr,0):
realloc(ptr,0) only gives NULL on alloc failure, ptr unchanged, errno is ENOMEM
Which I read as saying that ptr is not freed (I don't have a BSD system to check this on).
In other words, it's kind of a mess what should/will happen with realloc(ptr,0) - I wouldn't depend on it freeing the memory. Call free() of that's what you want to do.
I wonder what the rationale for C99 removing that sentence was...
In fact, the C99 standard actually seems to preclude realloc(ptr,0) from simply freeing the block (ie., it doesn't seem to leave this implementation defined at all). The C99 standard says:
... If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged
Returns
The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.
So, if realloc() returns NULL then the new object could not be allocated and the old object must not be freed. But if realloc() returns non-NULL, then you still have the object that must be freed (or some new memory object that must be freed). In other words, if realloc(ptr,0) returns NULL then you are still responsible for freeing ptr. If realloc(ptr,0) returns a non-null pointer, then you're responsible for freeing whatever pointer is returned.
I guess maybe that's why C99 removed that sentence. Note however, that many (most?) implementations do free the block when realloc(ptr,0) is called, regardless of what C99 might have to say about it.
"%p"to print pointers.