In section 6.3 of the book "Inside the C++ Object Model", Temporary Objects (page 230):
The actual results are implementation dependent based on how aggressive the underlying delete operator is in actually freeing the memory addressed. Some implementations, while marking the memory as free, do not actually alter it in any way. Until the memory is claimed by something else, it can be used as if it had not been deleted. While obviously not an exemplary approach to software engineering, this idiom of accessing memory after it has been freed is not uncommon. Many implementations of
malloc()
, in fact, provide a special invocationmalloc(0);
to guarantee just this behavior.
According to the above, malloc(0)
seems to be related to accessing memory which has already been freed (but the content of which has not been changed).
My question is how malloc(0)
can guarantee this behavior?