views:

830

answers:

5

Dynamic allocations with new/delete are said to take place on the free-store,
while malloc/free operations use the heap.

I'd like to know if there is an actual difference, in practice.
Do compilers make a distinction between the two terms? (Free store and Heap, not new/malloc)

+1  A: 

I don't recall the standard ever mentioning the word heap, except in the descriptions of heap functions like push_heap et al. All dynamic allocations are performed on the free-store.

avakar
Yes - sorry, I haaven't had breakfast yet :-(
anon
+3  A: 

The term "heap" may also refer to a particular data structure, but in the context of the C++ malloc, free, new, and delete operations the terms "heap" and "free store" are used more or less interchangeably.

Jim Lewis
+9  A: 

See http://www.gotw.ca/gotw/009.htm; it can describe the differences between the heap and the free-store far better than I could:

Free-store:

The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

Heap:

The heap is the other dynamic memory area, allocated/freed by malloc/free and their variants. Note that while the default global new and delete might be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

Mike Koval
I disagree. The word "heap" in the context of dynamic allocation is used neither by the C++ standard nor C99 (I don't have C89 to which C++ refers, feel free to correct me if it uses the word). I couldn't find the date the GotW in question was published, but since it talks about the draft, it's obviously pre-standard.
avakar
+2  A: 

Mike Koval's answer covers the theory quite well. In practice, however, they are almost always the same region of memory -- in most cases if you dig into the compiler's implementation of new, you'll find it calls malloc().

In other words: from the machine's point of view, heap and free store are the same thing. The distinction exists inside the compiler.

To make things even more confusing, before the advent of C++ we said "heap" to mean what is now called "free store."

Crashworks
A: 

You can also check the discussion here:

What's the difference between the heap and the freestore?

Naveen