malloc does not return null when out of memory

Malcolm Kay malcolm.kay at internode.on.net
Thu Jul 24 09:43:39 PDT 2003


On Thu, 24 Jul 2003 23:11, Gabor wrote:
> On Wed, Jul 23, 2003 at 02:09:00PM -0700, Doug White wrote:
> # On Wed, 23 Jul 2003, Gabor wrote:
> #
> # > We have a little soekris box running freebsd that uses racoon for key
> # > management.  It's used for setting up an ipsec tunnel.  I noticed that
> # > one of these devices lost the tunnel this morning.  I looked in the
> # > log and saw this
> # >
> # > Jul 23 01:37:57 m0n0wall /kernel: pid 80 (racoon), uid 0, was killed:
> out of swap space #
> # [...]
> #
> # > Here is the tail end of the output.  It dies when trying to poke at
> # > the memory using memset.  If I just malloc without the memset, it
> # > never even dies.
> #
> # That's normal. If you malloc() memory and never touch it then it is never
> # actually allocated.
>
> It's not normal.  If malloc cannot allocate memory it should return a
> null pointer.  How would my application know there is no more memory?
> Dying is not a very good behavior.  The C standard and even the man
> page says that malloc will return null when there is no more memory to
> be had by the process.

While this might be a desirable state of affairs I can't find any statement 
in the (draft) standard or the man page that malloc returns null when there is 
no more memory (whatever that might mean) . The (draft) standard says it 
either returns a pointer to allocated memory or null, without any reason 
given for the different behaviours. The man page merely says null if the 
call fails without reference to why.

Malcolm Kay




More information about the freebsd-stable mailing list