> As I have understand, Linux returns ALWAYS success when using malloc(),
> because only reason why malloc() would fail, is memory overrun, and this
> will never happen in malloc().
> So my question is: is there any point in checking whether malloc() returned
> NULL (failure) or success? Should i just start using the memory without
> checking if the pointer is NULL?
> | Tuukka Toivonen <email@example.com>
> | Homepage: http://stekt.oulu.fi/~tuukkat/
> | Try also finger -l firstname.lastname@example.org
> | Studying information engineering at the University of Oulu
Not true. Malloc and friends will return a NULL pointer when it is
no longer able to set a new break address, i.e., get more memory from
the kernel. You can try it yourself....
long mem = 0L;
ptr = malloc(0x10000);
if(ptr == NULL)
fprintf(stderr, "No memory %d\n", mem);
*ptr = 0xaa; /* dirty memory */
mem += 0x10000;
Now, if you don't write to the memory, the allocation still fails at
what seems to be around the page-file size + 4 megabytes * 3. If
you do write to memory, the allocation fails at about the page-file
size + 4 megabytes. I have 32 megabytes of RAM so your mileage may
It looks as though "over commitment" should not be a problem. If memory
allocation fails, the program should have some way of handling the
situation, i.e., writing primary results out to disk files, etc. Too
many so-called Software Engineers think that the way to sort something,
for instance, is to load the entire database into RAM. Sorry. I have
a database you will NEVER fit into RAM. There just isn't that much
RAM in the world (--yet, but they are working on it).
Richard B. Johnson
Voice : (508) 977-3000 ext. 3754
Fax : (508) 532-6097
Modem : (508) 977-6870
Ftp : email@example.com
Email : firstname.lastname@example.org, email@example.com
Penguin : Linux version 2.1.26 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.