This is how it must behave according to the C standard. However, I am
told that Linux behaves differently, in a possibly non-conforming
manner: the memory is not actually allocated until you try to use it, at
which point there may no longer be free memory left.
There was a thread about lazy allocation here not long ago, and the
consensus appears to be that lazy allocation is a QoI issue (and a
debatable one at that), not a conformance one. See [1] (downthread
a ways). (I don't know about typical Linux implementations, but AIX,
for example, provides a lazy allocator by default, though there are
ways to select a strict allocator and require backing store for all
allocated memory.)
Consider that allocated memory may become unavailable after allocation,
even without lazy allocation, eg due to hardware failure in the portion
of the disk used for paging space in a virtual-memory OS. A non-null
return from malloc should not be interpreted as a guarantee from the OS
that the program will never run into difficulties due to accessing that
memory, just as a non-null return from fopen does not guarantee that
the program will always be able to perform I/O successfully on that
file.
1.
http://groups.google.com/[email protected]&rnum=1
--
Michael Wojcik (e-mail address removed)
Pocket #9: A complete "artificial glen" with rocks, and artificial moon,
and forester's station. Excellent for achieving the effect of the
sublime without going out-of-doors. -- Joe Green