delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1992/06/05/12:38:03

Date: Fri, 5 Jun 92 09:58:36 MDT
From: edsr!tantalum!pete AT uunet DOT UU DOT NET (Pete Humphrey)
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: malloc() doesn't return a null pointer when out of memory

>  > > It is supposed to keep allocating memory until there is no more memory
>  > > left, at which point I expected to get a NULL pointer returned from
>  > > malloc(). 
>  > This is really nasty: allocating more and more memory. You will get
>  > real enemies when you try this on a Unix machine...
>  
>  That's besides the point.  Malloc() should return NULL when there is no
>  more memory to be allocated.  It should not give a segmentation fault
>  nor should one need to get a larger disk for swap space.  Why should
>  one make life hard for oneself when one can rely on the simple fact
>  that malloc() return NULL when there is no more memory and then take
>  it from there?

I agree, and so do the docs.  It's one thing to work around bugs, but we
shouldn't promote them to features just because we think it's hard to do it
right.

>  
>  > Since gcc is a Unix compiler, it is thought to give you as much memory as
>  > you need. It should *never* let you run out of memory. Especially malloc
>  > cannot see when your disk is full...

Dream on.  Better yet, read the man page.  Try it on a unix system.  You
will find it does run out of memory and does return NULL.  Virtual memory
doesn't mean unlimited memory.

[ stuff deleted ]

>  
>  > > Shouldn't malloc just return a NULL pointer?  I have some
>  > > memory-hogging programs that depend on that for error checking.
>  > This is the usual behaviour on DOS machines. You are on the way to
>  > Unix :-) :-)

Wrong.  See previous comment.

--				
Pete Humphrey			US Mail:
pete AT edsr DOT eds DOT com		EDS Research
(505) 345-1863			5951 Jefferson Street N.E.
(505) 345-3463 (fax)		Albuquerque, New Mexico 87109-3432

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019