23

I am trying to find the official (or a good enough) reason that the free store is commonly referred to as the heap.

Except for the fact that it grows from the end of the data segment, I can't really think of a good reason, especially since it has very little to do with the heap data structure.

Note: Quite a few people mentioned that it's just a whole bunch of things that are kind of unorganized. But to me the term heap physically means a bunch of things that are physically dependent on one another. You pull one out from underneath, everything else collapses on it, etc. In other words, to me heap sounds loosely organized (e.g., latest things are on top). This is not exactly how a heap actually works on most computers, though if you put stuff towards the beginning of the heap and then grew it I guess it could work.

Uri
  • 84,589
  • 46
  • 214
  • 312
  • 3
    Don't take 'heap' too literally. Trees have a single root but not in real life. Queues can only extract from the ends but, in real life, people can leave queues from the middle when they get tired of waiting. Obnoxious people can also push in. – paxdiablo Mar 19 '09 at 07:37
  • The free memory "heap" doesn't collapse, though (does it?). It can get fragmented as memory is allocated and deallocated. – Bill the Lizard Mar 19 '09 at 14:18
  • Related posts [here](https://stackoverflow.com/q/1699057/465053) and [here](https://stackoverflow.com/q/756861/465053). – RBT Jan 03 '18 at 22:36

9 Answers9

36

Knuth rejects the term "heap" used as a synonym for the free memory store.

Several authors began about 1975 to call the pool of available memory a "heap." But in the present series of books, we will use that word only in its more traditional sense related to priority queues. (Fundamental Algorithms, 3rd ed., p. 435)

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
  • 2
    Bah, Knuth. What would he know about data structures? :-) That's his decision of course but he's well in the minority, despite his fame. – paxdiablo Mar 19 '09 at 07:16
  • 3
    @Pax: Yes, "heap" definitely caught on, despite Knuth's effort. That seems to have something to do with its use in the C memory model, which would have been gaining momentum right around the time Knuth is referring to in the quote. – Bill the Lizard Mar 19 '09 at 13:57
  • 1
    @James M: I spent quite some time under the impression that memory was allocated in a heap data structure. – Bill the Lizard Aug 06 '09 at 18:25
  • **Timeline:** - **1968:** ALGOL68 has declarations like: ( **int** i; **ref int** r; **loc int** a; **long int** l; **int** c=1234; **short int** s; **short short int** ss; **long long int** ll; ~ ) AND ( **heap int** h; ~ ) - **1972:** C has declarations like "{ int i; int *r; auto int a; long int l; const int c=1234; static int s; extern int e; ...; } - – NevilleDNZ May 12 '10 at 12:27
  • N.B. I don't think it's right to blame C, as neither K&R (2nd Ed) nor the C standard say "heap" anywhere. The C++ standard only uses `heap` to mean the data structure, and refers to the source of dynamic memory as "the freestore". – Jonathan Wakely Jul 30 '18 at 16:49
20

For what it's worth, ALGOL68, which predated C, had an actual keyword heap that was used to allocate space for a variable from the "global heap", as opposed to loc which allocated it on the stack.

But I suspect the use may be simply because there's no real structure to it. By that, I mean that you're not guaranteed to get the best-fit block or the next block in memory, rather you'll take what you're given depending on the whims of the allocation strategy.

Like most names, it was probably thought of by some coder who just needed a name.

I've often heard of it referred to as an arena sometimes (an error message from many moons ago saying that the "memory arena was corrupted"). This brings up images of chunks of memory doing battle in gladiatorial style inside your address space (a la the movie Tron).

Bottom line, it's just a name for an area of memory, you could just as well call it the brk-pool or sbrk-pool (after the calls to modify it) or any of a dozen other names.

I remember when we were putting together comms protocol stacks even before the OSI 7-layer model was a twinkle in someone's eye, we used a layered approach and had to come up with names at each layer for the blocks.

We used blocks, segments, chunks, sections and various other names, all which simply indicated a fixed length thing. It may be that heap had a similar origin:

Carol: "Hey, Bob, what's a good name for a data structure that just doles out random bits of memory from a big area?"

Bob: "How about 'steaming pile of horse dung'?"

Carol: "Thanks, Bob, I'll just opt for 'heap', if that's okay with you. By the way, how are things going with the divorce?"

Community
  • 1
  • 1
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
17

It's named a heap for the contrasting image it conjures up to that of a stack.

  • In a stack of items, items sit one on top of the other in the order they were placed there, and you can only remove the top one (without toppling the whole thing over).

    Stack like a stack of papers

  • In a heap, there is no particular order to the way items are placed. You can reach in and remove items in any order because there is no clear 'top' item.

    Heap like a heap of licorice allsorts

It does a fairly good job of describing the two ways of allocating and freeing memory in a stack and a heap. Yum!

Community
  • 1
  • 1
thomasrutter
  • 104,920
  • 24
  • 137
  • 160
3

It's unordered. A "heap" of storage.

It isn't an ordered heap data structure.

S.Lott
  • 359,791
  • 75
  • 487
  • 757
  • Probably as opposed to the ordered stack. – Michael Mar 19 '09 at 02:34
  • I like to think of it as an unordered pile of , aka a "heap," like you pointed out. – Cᴏʀʏ Mar 19 '09 at 03:02
  • But see, when I think of a physical heap of crap (like my desk), I don't see random memory, I see something that has a structure since every item lies on top of other items... – Uri Mar 19 '09 at 03:40
  • You aren't deleting from the middle of the things on your desk and freeing up gaps are you? Is the pile if crap on your desk actually a stack -- last on first off? Heap memory allocation creates gaps in the middle from deletes. – S.Lott Mar 19 '09 at 09:54
3

I don't know if it is the first but ALGOL 68 had keyword 'heap' for allocating memory from the free memory heap.

The first use of 'heap' is likely to be found somewhere between 1958, when John McCarthy invented garbage collection for Lisp and the development of ALGOL 68

Arnold Spence
  • 20,892
  • 7
  • 69
  • 66
2

It's a large, tangled pile of disorganized junk, in which nothing can be found unless you know exactly where to look.

Crashworks
  • 38,324
  • 12
  • 96
  • 168
1

I always figured it was kind of a clever way of describing it in comparison to the stack. The heap is like an overgrown, disorganized stack.

Jon B
  • 48,211
  • 29
  • 125
  • 158
0

Just like java and javascript, heap (free store) and heap (data structure) are named such to insure that our fraternity is inpenetrable to outsiders, thus keeping our jobs secure.

James McMahon
  • 45,276
  • 62
  • 194
  • 274
0

Heap usually has these features:

  1. You can't predict the address of a block malloc() returns.

  2. You have no idea of how the address returned by the next malloc() is related to the address of the previous one.

  3. You can malloc() blocks of variable sizes.

So you have something that can store a bunch of blocks of variable sizes and return them in some generally unpredictable order. How else would you call it if not "heap"?

sharptooth
  • 159,303
  • 82
  • 478
  • 911
  • According to dictionaries "pool" is more suitable for uniform objects. Chunks in the free store can have variable sizes and therefore are not very uniform. – sharptooth Mar 19 '09 at 08:43