G
glen herrmannsfeldt
On Monday, March 10, 2014 5:43:18 AM UTC-7, Martin Shobe wrote:
(snip)
(snip)
But, the question that I am asking is whether free is allowed to
not deallocate memory. The person who prompted me to ask this
question in the first place claimed that because the program in
question could not, using only the semantics of the C Abstract
machine, determine whether free() had done anything, and therefore,
free() need not do anything, even if it meant a massive memory
leak on a program which did appropriately call free() for all
memory it allocated.
and the standard leaves the amount of available memory up to the
implementation, and often on whatever else the machine is doing
at the time.
I have two issues with saying that deallocation can be delayed.
One is that the standard says that all side effects must be
completed before the last sequence point of a full expression.
Thus, for the side effect of free to be delayed seems outside
of the standard.
As far as I know, that is for side effects visible to the program.
Note that printf() has the side effect of printing something somewhere,
such as a terminal or line printer, but that effect usually happens
much later, if ever.
Secondly, the standard does say that free() makes the space
available for further allocation. However, if the implementation
for example delayed freeing until it ran out of memory then freed
on demand, (Android style LOL) then, for all practical purposes,
all calls to free() would have the same effect as if free had
fully freed within its own sequence points.
However, I do not see the letter or the spirit of the standard
allowing free() to have a delayed effect. Obviously, malloc() is
not allowed to postpone its creation of an object otherwise
immediate use of that object would be bad.
Seems that many systems now have lazy allocation, also known as
overcommit, where malloc() never returns NULL. Similar to the way
airlines overbook expecting some no-shows, lazy allocation assumes
that you malloc() more than you really need.
allocating and deallocating are the same class of side effects.
On many systems, the available memory depends on what other programs
are using, so the side effects of malloc() and free() are, to some
extent, outside the control of the program. Seems to me that makes
them different.
-- glen