No, it is conformance, and returning non-NULL would be non-conformance.
I disagree (at least) about the second member of that answer under
C99. IMHO realloc(p,0) is allowed to return the same thing as
malloc(0), which is not required to return NULL.
I reason that when p is a valid non-NULL pointer to a pointer returned
by malloc() and memory is aplenty, realloc(p,0) must return a pointer
to a new object that has the size 0, which is the same thing a what
malloc(0) should return, which is not required to be NULL, and is not
NULL in many implementations, including that one I'm strugling with.
C89 4.10.3.4 The realloc Function
If size is zero and ptr is not a null pointer, the object
it points to is freed.
Returns
The realloc function returns either a null pointer or a
pointer to the possibly moved allocated space.
In the case of zero as the size, there is no more allocated space
(because zero size requires freeing), so the other branch of the
'either' kicks in, requiring realloc to return a null pointer
for this case.
C99 says {
void *realloc(void *ptr, size_t size);
The realloc function deallocates the old object pointed to by ptr and
returns a pointer to a new object that has the size specified by size.
The contents of the new object shall be the same as that of the old
object prior to deallocation, up to the lesser of the new and old
sizes. Any bytes in the new object beyond the size of the old object
have indeterminate values.
If ptr is a null pointer, the realloc function behaves like the malloc
function for the specified size. Otherwise, if ptr does not match a
pointer earlier returned by the calloc, malloc, or realloc function,
or if the space has been deallocated by a call to the free or realloc
function, the behavior is undefined. If memory for the new object
cannot be allocated, the old object is not deallocated and its value
is unchanged.
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.
}
I get the same result "realloc(p,0) returned NULL" with the source
changed to the deeper test:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
void *p = malloc(1);
if (p==NULL)
puts("Memory is very low"); // does not occur
if (malloc(0)==NULL)
puts("malloc(0) returned NULL"); // does not occur
if (realloc(NULL,0)==NULL)
puts("realloc(NULL,0) returned NULL"); // does not occur
if (realloc(p,0)==NULL)
puts("realloc(p,0) returned NULL"); // occurs ?!
if (malloc(10000)==NULL)
puts("Memory was rather low"); // does not occur
}
So I get an implementation of realloc(ptr,0) that turns a NULL ptr to
a non-NULL result, and non-NULL ptr to NULL result; which is at the
very least a surprise, non-orthogonal, and error-prone. In my case a
perfectly sound-looking "read the whole file in memory, in several
chuncks as necessary" function failed for an empty file, claiming
there is no memory.
Francois Grieu