Or even:
if (len == 0)
return null_buffer_singleton; /* special shared representation for 0: */ if (len == 0)
return null_buffer_singleton; /* special shared representation for 0: */ void buffer_free(void *buf)
{
if (buf != null_buffer_singleton)
free(buf);
}But frankly, having a 1-byte buffer, pointer to which can serve as a sentinel value à la NULL (but dereferenceable!), and which you can pass to free() without it being deallocated is indeed rather useful.