C++, Free-Store vs Heap
Asked Answered
D

7

154

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)

Danny answered 29/8, 2009 at 8:5 Comment(1)
On embedded system, yes there may be a distinction. On most (all?) personal computers, the new/delete free-store is a heap. (On my machine, new/delete, new[]/delete[], and malloc/free have separate heaps. But they're all heaps.)Sink
C
92

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.

Caelian answered 29/8, 2009 at 8:15 Comment(3)
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.Virchow
This is all the question of terminology, imho. Say, mr. Stroustrup does not distinguish 'heap' and 'free store': stroustrup.com/Programming/17_free_store.ppt, slide 12. 'Heap' was used as a synonym of dynamic memory long ago before C++, since Lisp time (1960s) which used heap data structure for memory allocation.Nitro
I generally think of the heap (via maloc/free) as a sort of 'raw' material supplier. You ask for a chunk of memory you get it no frills. You have to build any structures yourself. The Free Store (new/delete) is more like a 'finished goods' supplier. You ask for an object and it gets allocated some space, and the object it built up and prepared for your use. When it is finished with it gets nicely cleaned up.Sleuth
H
96

For C++, the difference between the free store and the heap has become purely conceptual. Like a jar for collecting bugs, and one for collecting cookies. One is labeled one way, the other another. This designation is meant to drive home the point that you NEVER mix "new" and "delete" with "malloc", "realloc", or "free" (or bit level sets for that matter).

During interviews it's good to say that "new and delete use the free store, malloc and free use the heap; new and delete call the constructor and destructor, respectively, however malloc and free do not." Yet, you will often hear that the memory segments are really in the same area - however, that CAN be compiler specific, that is to say, it is possible that both can designate different memory spaces as pools (not sure why it would be, though).

Hollishollister answered 8/2, 2012 at 12:18 Comment(0)
C
92

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.

Caelian answered 29/8, 2009 at 8:15 Comment(3)
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.Virchow
This is all the question of terminology, imho. Say, mr. Stroustrup does not distinguish 'heap' and 'free store': stroustrup.com/Programming/17_free_store.ppt, slide 12. 'Heap' was used as a synonym of dynamic memory long ago before C++, since Lisp time (1960s) which used heap data structure for memory allocation.Nitro
I generally think of the heap (via maloc/free) as a sort of 'raw' material supplier. You ask for a chunk of memory you get it no frills. You have to build any structures yourself. The Free Store (new/delete) is more like a 'finished goods' supplier. You ask for an object and it gets allocated some space, and the object it built up and prepared for your use. When it is finished with it gets nicely cleaned up.Sleuth
I
37

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."

Instruct answered 29/8, 2009 at 8:19 Comment(2)
Than calling new + delete is equivalent to calling new + destructor + free?Energumen
It's undefined behavior, because the standard doesn't say anything about whether new and malloc() draw from the same heap. In practice, most implementations of new just call malloc() underneath. But you can't count on that.Instruct
J
7

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.

Joelynn answered 29/8, 2009 at 8:14 Comment(0)
V
4

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.

Virchow answered 29/8, 2009 at 8:9 Comment(0)
I
4

Free Store is a pool of un-allocated heap memory given to a program that is used by the program for dynamic allocation during the execution of program. Every program is provided with a pool of un-allocated heap memory that it may utilize during the execution. This pool of available memory is referred to as free store of the program. The allocated free store memory is unnamed.

Iso answered 11/1, 2017 at 13:53 Comment(0)
J
3

Heap and free-store aren't supposed to be interoperable. In constrained contextes like in AVR 8-bit micro controllers with c++11 Standard Library, they cannot even be used in the same program. Free store and heap do their allocations in the same memory space, overwriting each other structures and data. In this context, Free store is different and incompatible with Heap because the "new/delete free store library" is simpler (and quicker) than the "Malloc/free/realloc/calloc heap library" and thus provides huge memory usage gains to the C++ embedded programmer (in a context where you have only 512 bytes of RAM).

See 8-bit c++11/14 Standard Library at https://github.com/ambroise-leclerc/ETL/tree/master/libstd

Jacalynjacamar answered 3/5, 2014 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.