[snips]
Why? They will be brought back
No; they *may* be brought back. They may not be. No such allowance is
made; the requirement is that they *are*.
As well as there is zero guarantee about very next call to malloc(). All
you know is that it may fail.
Correct, it may. It may _not_, however, fail because the implementation
chose to hand the memory freed back to the OS. The standard forbids
this, meaning that a failure in a subsequent allocation (of the same or
lesser amount of memory) is at best a QoI issue, at worst a bug, whereas
handing the memory back to the OS such that the OS might re-use it is not
merely a QoI issue or a bug, but a non-conforming behaviour.
Yep. And a strictly conforming program can't know when that is going to
happen.
Not quite the point, though, is it? It knows the memory _will_ be
available, whereas if the memory is handed off to the OS for use outside
the scope of the program, the guarantee is violated.
No, the point is that a strictly conforming program can't possibly know
whether it's still Monday or that implementation gave the memory back to
OS. So no strictly conforming program can demonstrate that this
implementation gave memory back to OS.
It doesn't need to. Try the following:
void *ptr = malloc( 1000 ); /* this one worked, error checked omitted */
free( ptr );
ptr = NULL;
while ( ptr == NULL )
{
ptr = malloc( 100 );
}
According to the standard, this program should fall out of the loop at
some point, whether today or tomorrow or next week, as the memory has
been allocated once, freed, thus made available.
The only cases where this is not true are:
1) Where the implementation's memory management is so bad it can't
allocate a 100-byte pool from a reserved 1,000-byte arena
2) Where the implementation's memory manage is so bad that the overhead
of the subsequent allocations exhaust the pool of previously allocated
memory
3) The implementation does something really asinine, such as handing the
memory back to the OS, such that it can no longer guarantee the program
will behave in a manner defined by the standard.
The first two are QoI issues, the latter is nothing less than a non-
conforming implementation.
Assuming a decent quality memory manager in the implementation, and that
the implementer actually read the standard, the loop _must_ exit, whether
the first time or the thousandth.
If an implementation accepts and correctly executes every strictly
conforming program (plus it does what it should with incorrect programs,
like issues diagnostics), is it conforming?
If it's handing back memory to the OS when free is called, then it is not
correctly executing ever strictly conforming program, unless "strictly
conforming program" excludes those which dynamically allocate memory, in
which case the malloc failure discussed is irrelevant.
If dynamic memory allocation _is_ part of a strictly conforming program,
then the behaviour of free is strictly curtailed; the loop above *must*
terminate.