Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 13
    The fact that it is implementation dependent makes it more or less completely useless - this is one of the crappier bits of the C standard, and quite a few of the standards comittee (for example P.J. Plauger) have moaned about it. Commented Jan 7, 2010 at 17:56
  • 10
    I agree. If malloc(0) returned a valid pointer, then malloc() returning NULL means "failure" always, and 0 isn't a special case anymore, which is more consistent. Commented Jan 7, 2010 at 17:57
  • 1
    Since the circumstances of malloc failure to obtain memory are implementation-defined, an implementation could simply define that size-0 allocations are always unsatisfiable (ENOMEM), and now malloc(0) returning 0 (with errno==ENOMEM) is consistent. :-) Commented Sep 6, 2011 at 3:53
  • 8
    Can you realloc a pointer returned by malloc(0)? Can you realloc((char*)NULL)? Commented Aug 7, 2016 at 21:21
  • 4
    @Braden Best Yes to both. Commented Sep 12, 2016 at 22:35