malloc does not return null when out of memory

Andrew Reilly areilly at bigpond.net.au
Wed Jul 23 23:58:39 PDT 2003


Mike Tancsa wrote:

> At 08:15 PM 7/23/2003 -0700, Kris Kennaway wrote:
>
>> On Wed, Jul 23, 2003 at 01:34:27PM -0400, Gabor wrote:
>>
>> > Here is the tail end of the output.  It dies when trying to poke at
>> > the memory using memset.  If I just malloc without the memset, it
>> > never even dies.
>>
>> Ah, the annual "memory overcommit" thread.  I thought we were overdue
>> for one.
>
>
>
> But why does the man page for malloc (3) say,
>
>     If malloc() fails, a NULL pointer is returned.


Because that's what happens.  See the subthread with the ulimit examples.

In the cases being cited, the malloc itself is not failing, because it 
is able to return a pointer to a chunk of *VM*.  The problem occurrs 
later, when the program attempts to read or write to that memory, and it 
doesn't occur because malloc was wrong, but because no other process has 
exited or unmapped something in the mean-time, to free up a page to back 
that address space.

--
Andrew



More information about the freebsd-stable mailing list