[Bug 1730] Change malloc(0) behavior to match FreeBSD and Linux

bugzilla-daemon at rtems.org bugzilla-daemon at rtems.org
Mon Jan 31 22:22:42 UTC 2011


https://www.rtems.org/bugzilla/show_bug.cgi?id=1730

--- Comment #14 from dufault at hda.com 2011-01-31 16:22:40 CST ---
(In reply to comment #13)
> Interestingly, I ran into code today that failed because of this behavior. The
> code uses lists allocated within a struct, and allocates a list of size 0 in
> case the list is unused by the struct.  The check for NULL return value for
> out-of-memory was tripped by malloc(0), so the program spuriously exited.  I
> had to add a check to say that I wanted to use malloc(0)'s results, and another
> check when free'ing to avoid passing NULL.

By definition you can pass 0 to free() and realloc() and they will do the right
thing, so the second check isn't needed.  You need the first "malloc(n) == 0 &&
n" instead of just "malloc(n) == 0" to indicate out of memory.  The modified
code has the advantage of being portable.  For better or for worse, malloc(n)
== 0 is not defined to mean "out of memory".

-- 
Configure bugmail: https://www.rtems.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are watching all bug changes.



More information about the bugs mailing list