The C specification says this about the memory allocation functions:
If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
Do all the allocation functions have to be consistent in this regard? Can malloc(0)
return a non-null pointer, while realloc(someptr, 0)
returns a null pointer?
Can calloc(0, 0)
be different from malloc(0)
? It would have been simple for the calloc()
specification to say that calloc(nmemb, size)
is equivalent to malloc(nmemb * size)
followed by zeroing the memory, but this equivalence is not explicit.