Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is malloc(0) useful in "Inside the C++ Object Model?"

Tags:

c++

memory

malloc

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 invocation malloc(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?

like image 502
user571470 Avatar asked Dec 25 '12 03:12

user571470


2 Answers

EDIT: I did not notice that "inside the C++ object model" was a reference and not merely part of the question title.

As Dietmar Kuhl says, it looks like a kludge and you shouldn't rely on such an extension even it appears on your platform.

"Most platforms" do include some kind of malloc debugging facility to help programmers find code that accesses memory already passed to free. This can involve performing memory management less lazily or overwriting with special byte pattern. It sounds like that text is describing malloc(0) as a means of disabling such a facility. On Mac OS X I recall there is an environment variable (something like MALLOC_DEBUG) which controls this. What the facility does and how it is enabled will vary a lot between platforms. Overriding malloc(0) is neither common nor a good interface.

EDIT: I found it in Inside the C++ object model by Stanley Lippman, 1996. Not an authoritative source. That entire page appears to be devoted to discussing differences between standard and "pre-standard" platform implementations, although 1996 was also before the first standard was finished. Be aware that was a long, long time ago in computer years and unless you're bringing a specific app back from the dead, such information will be totally irrelevant. Lippman apparently released a new book on C++11, which includes an updated object model with multithreading support. (Although I don't know whether he or that book is any good.)

like image 140
Potatoswatter Avatar answered Nov 20 '22 01:11

Potatoswatter


From the quote it seems that there are implementations of malloc() which use malloc(0) as some sort of switch to indicate that future calls to malloc() allocate memory in a way that free() (and probably realloc(p, 0)) won't change the content of the allocate memory when marking it as unused. This is, clearly, a standard conforming extension which you'd better not rely on.

like image 23
Dietmar Kühl Avatar answered Nov 20 '22 03:11

Dietmar Kühl