malloc(0) and free(NULL)

The C99 standard defines that a malloc(0) is legal:

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.

If a non-zero value is returned it should be not used but it is legal to pass it to a subsequent call of free (note that free(NULL)is also legal).

It follows that

  void * p = malloc(0);
  free(p);

is legal and should be accepted by your allocator.

0 Responses to “malloc(0) and free(NULL)”


  1. No Comments

Leave a Reply