malloc: bug or feature ?

Georges-Andre Silber silber at
Wed Apr 7 05:41:04 PDT 2004


On my bi/opteron HDAMA workstation running FreeBSD 5.2-CURRENT AMD64 
with 8GB RAM + 16GB Swap, I have a strange result for a test program I 
wrote in C:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define ITEM    100000000L
main (int ac, char* av[])
         unsigned long int memsize;
         void *mem;
         memsize = 0UL;
         while (1) {
                 mem = malloc (ITEM);
                 assert (mem != NULL);
                 memsize += ITEM;

As you can see, I just call malloc() until it returns NULL. The problem 
is that when total memory allocated reaches a value close to MAXDSIZE, 
it leads to a 'segmentation fault'.

I ran the program through gdb:

-bash-2.05b$ gcc -g taille.c
-bash-2.05b$ ls
a.out           taille.c
-bash-2.05b$ gdb a.out
GNU gdb 20040323 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain 
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-portbld-freebsd5.2"...
(gdb) run
Starting program: /home/silber/a.out

Program received signal SIGSEGV, Segmentation fault.
0x0000000200eec000 in ?? ()
(gdb) up
#1  0x0000000200681d79 in ldexp () from /lib/
(gdb) up
#2  0x00000002006821fd in ldexp () from /lib/
(gdb) up
#3  0x00000002006823e1 in ldexp () from /lib/
(gdb) up
#4  0x0000000200682789 in ldexp () from /lib/
(gdb) up
#5  0x0000000200682840 in malloc () from /lib/
(gdb) up
#6  0x00000000004006a9 in main (ac=1, av=0x7fffffffec38) at taille.c:12
12                      mem = malloc (ITEM);


tcsh> limit
cputime         unlimited
filesize        unlimited
datasize        8388608 kbytes
stacksize       524288 kbytes
coredumpsize    unlimited
memoryuse       unlimited
vmemoryuse      unlimited
descriptors     11095
memorylocked    unlimited
maxproc         5547
sbsize  unlimited
tcsh> uname -a
FreeBSD 5.2-CURRENT FreeBSD 5.2-CURRENT #2: Wed 
Apr  7 13:20:40 CEST 2004 
root at  amd64

Is it a 'normal' behaviour of malloc()? Shouldn't malloc() return NULL?

Georges Silber.

More information about the freebsd-amd64 mailing list