malloc does not return null when out of memory
Pete French
pfrench at firstcallgroup.co.uk
Thu Jul 24 06:54:47 PDT 2003
> 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?
But the point is that is *can* allocate the memory. Allocating it
is not the same as being able to use it. The behaviour you want is for the
system to not only allocate the memory, but to also ensure that the total
amount allocated by the system can never exceed the amount the system has
available.
> page says that malloc will return null when there is no more memory to
> be had by the process. It shouldn't return a bogus pointer so I can
Its not bogus - the trouble is that you cant tell at the time malloc returns
whether the pointer will be useable or not. You only find that out when
you try and use it, and whether theres any space or not depends oon what
else may have munched up (or released) memory between you making the call
to malloc() and actually writing to the location returned.
-pcf.
More information about the freebsd-stable
mailing list