25

Originally a pedantics war on @Als answer here, it also sparked a discussion in the C++ chatroom.

This article by Herb Sutter distinguishes between the two, but is also over a decade old, as it clearly was written before 2000 and also talks about the standard draft, which can only mean C++98 draft. Though, I still expect Herb, as part of the committee, to be knowledgeable about this stuff.

I know of this and this question/answer, but the second just cites Herb's article while the first cites another source that simply rejects the use of the term "heap", aka they are both not exactly satisfactory. Also, I can't find any free-store or freestore tag on SO (until this question, I created the former one now).

Now, what is really the difference, if any?

trincot
  • 317,000
  • 35
  • 244
  • 286
Xeo
  • 129,499
  • 52
  • 291
  • 397

3 Answers3

23

Well, the current C++ standard only uses the term "free store" - the only use of "heap" in the Standard is to describe the heap data structure in the Standard Library. So "heap" is not a very useful term to use when trying to discuss C++ problems accurately, though of course everyone does it.

  • 2
    Well, there was ever one occurrence of "heap" in some informal text of C++14 draft and I [wiped it out](https://github.com/cplusplus/draft/pull/380). – FrankHB May 08 '16 at 14:22
17

In Herb's book "Exceptional C++", he defines:

Heap: A dynamic memory area that is allocated/freed by the malloc/free functions.

Free Store: A dynamic memory area that is allocated/freed by new/delete.

Its possible for new and delete to be implemented in terms of malloc and free, so technically, they could be the same memory area. However, as the standard doesn't specify this, its best to treat them separatly, and not to mix malloc/delete or new/free.

Node
  • 3,443
  • 16
  • 18
6

The free-store technically isn't the heap, just as local variables technically aren't on the stack. However, it's extremely rare in my experience to find anybody who won't accept those terms.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    please clarify what is the meaning of "local variables technically aren't on the stack". – Eight Jun 28 '12 at 09:56
  • 3
    @abhinav8: C++ is a high-level programming language that abstracts away such implementation details as physical memory location. Thus, when talking about only C++, _there is no stack_. – Lightness Races in Orbit Jan 19 '13 at 17:46