Re: automatic vs allocated attempts



On 1/4/2010 3:41 PM, Richard Bos wrote:
ram@xxxxxxxxxxxxxxxxxx (Stefan Ram) wrote:

Why does C have this asymmetry between automatic and
allocated storage?

Why is it ok to code in C as if allocations of automatic
storage will never fail, but not ok to code as if
allocations of allocated storage will never fail, when
in fact both might fail?

Hysterical raisins, I presume. That is, it must have been assumed (and
was, AFAIK, common practice in pre-existing languages) that local
storage would only or predominantly be used for small objects (a handful
of single ints, floats or pointers) while static, global or allocated
storage would be used for large arrays and whole blocks of memory.

It's also the case that the mechanisms for detecting and
reporting (and maybe recovering from) exhaustion of automatic
memory are highly system-specific, varying rather widely from
one environment to the next. Even on a single machine, the
mechanisms available to hosted "user-land" code are usually
different from those applicable in free-standing "kernel" code.
That makes it difficult for a programming language to legislate
one particular way of handling the condition.

malloc(), on the other hand, is not really a creature of
the "core language," but of the library. It can report failure
as a function value -- the fact that functions can return values
is in no way a "special mechanism" just for memory management.
The details of detecting dynamic memory exhaustion can be hidden
safely inside the malloc() implementation, without putting a
structural burden on the rest of the language.

Even so, some implementations get malloc() arguably wrong.
"OOM pa-pa, OOM pa-pa, that's 'ow it goes ..."

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Re: Author of Visual C++ 2005 STL ???
    ... In VS6, release, each mallocconsumes 40 bytes ... smaller than 8 bytes, the storage is lost. ... had 100 bytes of free storage I could get 100 allocations? ... by other memory allocations. ...
    (microsoft.public.vc.mfc)
  • Re: [RFC 0/3] Recursive reclaim (on __PF_MEMALLOC)
    ... no memory and debug all the fallback paths in the kernel when an OOM ... longer time period where these allocations can fail. ...
    (Linux-Kernel)
  • Re: Is MFC reusing memory?
    ... Regarding the indication the memory is not freed: ... in the debug version. ... We are talking about allocations in C++, ... Read my essay on storage allocation. ...
    (microsoft.public.vc.mfc)
  • Re: automatic vs allocated attempts
    ... allocations of allocated storage will never fail, ... malloc can potentially give you access to the full memory resources ... whereas stack space is ``artificially'' limited. ...
    (comp.lang.c)
  • Re: [RFC 0/3] Recursive reclaim (on __PF_MEMALLOC)
    ... no memory and debug all the fallback paths in the kernel when an OOM ... longer time period where these allocations can fail. ...
    (Linux-Kernel)