malloc does not return null when out of memory

Barney Wolff barney at databus.com
Wed Jul 23 15:48:49 PDT 2003


On Wed, Jul 23, 2003 at 03:22:30PM -0700, Darryl Okahata wrote:
> Barney Wolff <barney at 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 at 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.


More information about the freebsd-stable mailing list