A solution for the allocation failures problem

E

Eric Sosman

Kelsey said:
(It was actually Keith Thompson, but the attributions have
been mangled. Or perhaps reallocated ...)
He said it; he couldn't thus far show where, in the standard, this
behaviour is allowed.

You may notice that in several places the Standard restricts
the behavior of the implementation w.r.t. library functions that
have internal state. "The implementation shall behave as if no
library function calls the rand function," and similarly for
srand and getenv and setlocale and various others.

You may also notice that the Standard does *not* similarly
restrict the implementation's use of malloc. That is, the
implementation is permitted to allocate dynamic memory when it
feels like doing so, holding on to it as long as it likes, and
freeing or not freeing it at its own whim. It seems likely
that fopen might well allocate some buffers and things, and it
seems all but certain that setvbuf would do so if passed a null
pointer.

The implementation might use dynamic memory for auto variables,
particularly for C99's variable-length arrays. That is, the
implementation might choose to allocate memory even when there's
no overt library function call in the vicinity.

The upshot is that it's difficult if not impossible to tell
that an implementation has failed to make freed memory "available
for further allocation." You can free a bunch of memory and turn
right around and try to allocate it again and discover that the
allocation attempt fails, but from this you cannot conclude that
the implementation has misbehaved. Some hard-to-see piece of the
implementation may have already taken advantage of the memory's
availability and allocated it for the implementation's own use,
making it unavailable to an ordinary malloc call, and this would
be entirely legal.

In short, even if we read the Standard as imposing such a
requirement, the requirement is untestable and hence without
force. An implementation that deliberately refused to re-use
freed memory, or which imposed a delay between the freeing and
any reallocation (as a debugging aid, say) could not haled into
court and convicted: it would still have "plausible deniability"
to any charge you might bring against it. If the requirement
exists, it might as well not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top