190

Why are the runtime heap used for dynamic memory allocation in C-style languages and the data structure both called "the heap"? Is there some relation?

Rob Kennedy
  • 156,531
  • 20
  • 258
  • 446
Andrey Fedorov
  • 7,836
  • 20
  • 63
  • 95
  • 4
    I was wondering this today while studying data structures. – MitMaro Nov 09 '09 at 04:16
  • 3
    Go to an English dictionary and count the number of entries under "Run". How many of the 40+ entries apply to computers? :) – jmucchiello Nov 10 '09 at 01:22
  • 2
    possible duplicate of [What's the relationship between "a" heap and "the" heap?](http://stackoverflow.com/questions/756861/whats-the-relationship-between-a-heap-and-the-heap) – RCIX Jun 14 '10 at 04:19
  • 1
    duplicate question? [http://stackoverflow.com/questions/756861/whats-the-relationship-between-a-heap-and-the-heap](http://stackoverflow.com/questions/756861/whats-the-relationship-between-a-heap-and-the-heap) – timB33 Nov 09 '09 at 09:54
  • A related post [here](https://stackoverflow.com/q/660855/465053) w.r.t. runtime heap used for dynamic memory allocation. – RBT Jan 03 '18 at 22:37

9 Answers9

88

Donald Knuth says (The Art of Computer Programming, Third Ed., Vol. 1, p. 435):

Several authors began about 1975 to call the pool of available memory a "heap."

He doesn't say which authors and doesn't give references to any specific papers, but does say that the use of the term "heap" in relation to priority queues is the traditional sense of the word.

James McNellis
  • 327,682
  • 71
  • 882
  • 954
  • 12
    Pool would be a better name than heap. –  Nov 09 '09 at 05:22
  • 7
    Interesting. Someone should ask him if he remembers which authors. – Prof. Falken Oct 18 '11 at 09:37
  • 28
    Wikipedia claims that it's because at an early stage Lisp used a heap (data structure) to implement its memory store. It doesn't say how. Its reference is "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest (1990): Introduction to algorithms. MIT Press / McGraw-Hill.", which I don't have. – Steve Jessop Jul 25 '12 at 09:04
  • 3
    I have no reference for this but my guess would be that initially the data structure used to organize references to open blocks of memory was a min heap. Seems like it would be at least a decent way of quickly finding the smallest block of memory that would allow you to store the data you were trying to store Update: What I said sounds exactly like buddy blocks http://en.wikipedia.org/wiki/Dynamic_memory_allocation#Buddy%5Fblocks – Will Jan 16 '13 at 22:37
  • 5
    @SteveJessop - Checking Cormen, Leiserson, Rivest, Stein - 3rd edition (2009) at the start of Heapsort chapter it only says 'The term "heap" was originally coined in the context of heapsort, but it has since come to refer to "garbage-collected storage," such as the programming languages Java and Lisp provide. Our heap data structure is not garbage-collected storage, and whenever we refer to heaps in this book, we shall mean a data structure rather than an aspect of garbage collection.' CLRS - 2nd edition also has almost exact same phrasing (no indication that Lisp used a Heap). – dr jimbob Feb 15 '13 at 22:18
  • Also http://en.wikipedia.org/wiki/Talk:Heap_(data_structure)#Origin_of_Phrase_.22the_Heap.22 discusses how there's lack of evidence for that old anonymous citation and the pages on dynamic memory allocation doesn't talk about lisp or Cormen et al any more. – dr jimbob Feb 15 '13 at 22:21
  • 2
    Donald is right and this is an unfortunate naming conflict. Particularly for non native speakers, e.g., yours truly, that their first encounter with the word "heap" was in their data-structure course. I have called it heap, always thought it was heap-like structure until today when I decided to understand how a heap-like structure is used for memory allocation. I'm mildly infuriated :/ – Pouya Aug 01 '17 at 08:31
69

They have the same name but they really aren't similar (even conceptually). A memory heap is called a heap in the same way you would refer to a laundry basket as a "heap of clothes". This name is used to indicate a somewhat messy place where memory can be allocated and deallocated at will. The data structure (as the Wikipedia link you reference points out) is quite different.

Andrew Hare
  • 320,708
  • 66
  • 621
  • 623
  • 8
    Yes, I think that's rather the point on which he's basing his question: they are different. So why are they called the same thing -- is there some underlying relation. – Sean Owen Nov 09 '09 at 04:25
  • 9
    The way I interpreted this answer is "no, there is no underlying relation", so it answers the question. – Laurence Gonsalves Nov 09 '09 at 04:52
  • Andrew is answering that. There's no relation. Just a coincidence. The memory heap is more true to the common usage since memory is allocated as if a "heap of clothes". The data structure however demanded a larger stretch of imagination. And this becomes a rather much more interesting "why". The name comes from the fact nodes are arranged by their key and a parent node key is always >= than its child node. – Alexandre Bell Nov 09 '09 at 04:57
  • 8
    They are definitely unrelated. However the problem with calling it "the heap" is that "the heap's" counterpart--"the stack"--is also an actual stack. – dan May 29 '12 at 22:19
  • 2
    I know why the heap data structure is called a heap: because it satisfies the heap property. But why is the heap property called such? It makes no sense to me, as a name like "top heavy" would be much better. – Thomas Eding Sep 18 '12 at 23:17
34

The name collision is unfortunate, but not all that mysterious. Heap is a small, common word used to mean a pile, collection, group, etc. The use of the word for the data structure pre-dates (I'm pretty sure) the name of the pool of memory. In fact, pool would have been a much better choice for the latter, in my opinion. Heap connotes a vertical structure (like a pile), which fits with the data structure, but not the memory pool. We don't think of a memory-pool heap as hierarchical, whereas the fundamental idea behind the data structure is keeping the largest element at the top of the heap (and sub-heaps).

Heap the data structure dates back to the mid-60s; heap the memory pool, the early-70s. The term heap (meaning memory pool) was used at least as early as 1971 by Wijngaarden in discussions of Algol.

Possibly the earliest use of heap as a data structure is found seven years earlier in
Williams, J. W. J. 1964. "Algorithm 232 - Heapsort", Communications of the ACM 7(6): 347-348

I. J. Kennedy
  • 21,946
  • 16
  • 59
  • 87
  • 2
    Yes, but a heap also implies disorder and memory heaps are generally disordered. The data structure heap is extremely well ordered. So again there's an equal mismatch going the other way based on the common definition of heap. – jmucchiello Nov 10 '09 at 01:24
  • It's always introduced as the opposite of *stack* which should suffice to explain the name IMO. – reinierpost Jan 07 '11 at 13:00
  • 1
    It's not coincidence -- the free list can be implemented as a priority queue via a binomial heap. – Heath Hunnicutt Jun 08 '11 at 20:26
  • 3
    @jmucchiello: a heap of logs (see [picture](http://www.shutterstock.com/pic-71326984/stock-photo-wood-exploitation-heap-of-logs-in-forest.html)) is well ordered and tree-resembling. This is the origin of the data structure's name according to one of my undergraduate textbooks. – gioele Oct 28 '11 at 10:56
6

Actually, reading about the way memory is allocated (see Buddy Blocks) reminds me of a heap in data structures.

Traveling Tech Guy
  • 24,425
  • 20
  • 100
  • 145
  • My comment on Peter Zhang's answer is also relevant here. The binary buddy system can be represented as a binary tree, and it also looks as a valid max heap does when the "key" of each node is the **total** memory underneath it (but these values are implicit and never change). Neither the allocation nor freeing algorithm use heap operations on this binary tree, as far as I can tell. – Eric Dubé Aug 28 '18 at 00:06
5

IMO it is merely an accident/coincidence that these two entirely unrelated things have the same name. Its like graph and graph.

MAK
  • 24,585
  • 9
  • 50
  • 82
  • The two graphs can though somehow be related. Imagine the graph of a function as follows: The tuple domain,range) is a vertex and a edge connects two such vertices –  Nov 09 '09 at 05:46
  • 2
    @Amit: For continuous graphs that would mean an infinite number of vertices. This is ok, but that also makes the concept of edges between the vertices meaningless. In the graph of the function f(x)=x*2, is there an edge between (0,0) and (1,2)? If yes, how about (0,0) and (0.5,1)? (0,0) and (0.25,0.5)? There is no way of having the concept of an edge between vertices, so this is not really a graph. – MAK Nov 09 '09 at 19:28
5

Heap-like data structure is used by algorithm of finding available memory allocation. The following is excerpted from http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html.

When new is invoked, it starts looking for a free memory block that fits the size for your request. Supposing that such a block of memory is found, it is marked as reserved and a pointer to that location is returned. There are several algorithms to accomplish this because a compromise has to be made between scanning the whole memory for finding the smallest free block bigger than the size of your object, or returning the first one where the memory needed fits. In order to improve the speed of getting a block of memory, the free and reserved areas of memory are maintained in a data structure similar to binary trees called a heap.

Peng Zhang
  • 2,988
  • 3
  • 25
  • 35
  • 1
    I'm extremely skeptical of this, specifically "... the free and reserved areas of memory are maintained in a data structure similar to binary trees called a heap." It sounds to me like the author is guessing there's a connection, based on the name "heap", and is probably mistaken. Can anyone confirm/refute? – Don Hatch Mar 26 '16 at 14:44
  • 1
    After some light research on the Binary Buddy system (used in Linux), it can be represented by a binary tree due to how it partitions data. This binary tree looks like a valid max heap if you observe the nodes in terms of total memory, but nodes aren't inserted into this binary tree as they are in a max heap - nodes are inserted directly in the smallest leaf of free memory >= the requested size. [1](https://www.kernel.org/doc/gorman/html/understand/understand009.html) [2](https://www.youtube.com/watch?v=1pCC6pPAtio) [3](https://www.youtube.com/watch?v=t0Cq6tVNRBA) – Eric Dubé Aug 27 '18 at 23:57
2

The colloquial terms stack memory and heap memory are not used in the C++ standard. The standard uses static storage, thread storage, automatic storage, and dynamic storage.

More can be found at Storage Duraction section of the standard.

Hence, from the language and standard library point of view, there is no confusion.

R Sahu
  • 196,807
  • 13
  • 136
  • 247
0

Q. What is a heap? A. A heap is a collection of objects place on top of each other.

Answer to your question: Both memory heap and binary heap uses the same concept as you know. Data is stored in the form of a heap in the memory in the same order as written in the program whereas binary heap is a data structure that follows the same concept of storing data in an ordered way in the form of a heap(Data on top of the other). Let me know what you think in the comments section.

  • 1
    _Both memory heap and binary heap uses the same concept as you know._ Memory heap and the heap data structure do not have anything in common – Kaiyaha Dec 01 '20 at 22:37
-3

Perhaps the first memory heap implemented was managed by a heap structure?

Adam Maras
  • 24,360
  • 5
  • 60
  • 89
  • 8
    That hypothesis doesn't seem at all obvious - how is a heap (the data structure) at all useful for maintaining a heap (the dynamic memory region)? – Keith Randall Nov 09 '09 at 04:42
  • 7
    -1. I would prefer an authoritative statement with evidence instead of what's obviously just a guess. – Rob Kennedy Nov 09 '09 at 07:19
  • Highly unlikely. There seems to be no good reason to use a heap (the data structure) to manage the heap (the pool of free memory). – jason Nov 09 '09 at 20:01