Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 23 Jul 2003 18:48:46 -0400
From:      Barney Wolff <barney@databus.com>
To:        Darryl Okahata <darrylo@soco.agilent.com>
Cc:        Gabor <gabor@vmunix.com>
Subject:   Re: malloc does not return null when out of memory
Message-ID:  <20030723224846.GB26555@pit.databus.com>
In-Reply-To: <200307232222.PAA26360@mina.soco.agilent.com>
References:  <20030723221336.GA26555@pit.databus.com> <200307232222.PAA26360@mina.soco.agilent.com>

next in thread | previous in thread | raw e-mail | index | archive | help

On Wed, Jul 23, 2003 at 03:22:30PM -0700, Darryl Okahata wrote:
> Barney Wolff <barney@databus.com> wrote:
> 
> > Shouldn't malloc return 0 when out of memory rather than returning
> > an address that won't work?  I believe that was the complaint.  Presumably
> > having NO_SWAPPING has something to do with it, but filling the swap
> > might well do the same thing.
> 
>      Well, it goes against the man page, but FreeBSD typically never
> returns NULL [*] when you run out of VM.  It's been this way for YEARS.
> 
> [*] -- Yes, it's annoying as h*ll, but that's the way it works.  I
>        thought that there was a FAQ or handbook entry on this, but I
>        couldn't find any.

This test prog appears to work correctly:
#include <stdio.h>
main() {
        char    *p;
        int             i=0;
        while (p=(char*)malloc(102400)) {
                *p = i;
                fprintf(stderr,"Ok after %d mallocs\n",++i);
        }
        fprintf(stderr,"Malloc returned 0 after %d calls\n",i);
        exit(0);
}
FreeBSD pit.databus.com 4.8-STABLE FreeBSD 4.8-STABLE #0: Tue Jul  8 23:22:41 EDT 2003     toor@pit.databus.com:/usr/obj/usr/src/sys/PIT  i386

ulimit -a says my data limit is 524288 KB, and the test says:
Malloc returned 0 after 5242 calls
When I ulimit -v 20000, malloc returns 0 after 191 calls.
So I believe things work correctly on my host, which does have swap although
the test does not use it.

-- 
Barney Wolff         http://www.databus.com/bwresume.pdf
I'm available by contract or FT, in the NYC metro area or via the 'Net.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030723224846.GB26555>