Heap
Noun · Development
Definitions
Heap is a term with two distinct meanings in computer science. As a data structure, a heap is a specialized tree-based structure that satisfies the heap property: in a max-heap, every parent node is greater than or equal to its children, and in a min-heap, every parent is less than or equal to its children. This property makes heaps ideal for priority queues, where the highest (or lowest) priority element must be efficiently accessible. Heapsort is a comparison-based sorting algorithm built on this structure. As a memory region, the heap is the area of RAM used for dynamic memory allocation, where programs request and release memory blocks at runtime using functions like malloc/free in C or new/delete in C++. Unlike the stack (which handles function calls and local variables with automatic cleanup), heap memory must be managed explicitly or through garbage collection. Memory leaks occur when heap-allocated memory is never freed.
In plain English: A data structure shaped like an upside-down tree where the most important item is always at the top; also the name for the area of memory where programs store data that lives beyond a single function call.
Example: "The priority queue uses a min-heap under the hood, so extracting the smallest element is always O(log n)."
Etymology
- 1964
- J.W.J. Williams invents the heap data structure and heapsort algorithm.
- 1970s
- Dynamic memory allocation systems adopt 'the heap' as the term for the region of memory managed by malloc/free.
- 1980s
- Heap corruption bugs become a major class of software defects in C programs. Debugging tools evolve to detect them.
- 2020s
- Garbage-collected languages (Java, Go, Python) manage the heap automatically. In Rust, ownership rules prevent heap corruption at compile time.
Origin Story
The Memory Free-for-All That Needs a Manager
The heap is a region of memory used for dynamic allocation, where blocks of memory can be requested and freed in any order at runtime. Unlike the stack, which follows a rigid last-in-first-out discipline, the heap is a free-form pool where memory can be allocated and deallocated in any sequence. The term comes from the mathematical concept of a heap as an unordered collection, reflecting the non-sequential nature of heap memory management. Early heap allocators appeared in the 1960s. The LISP programming language, created by John McCarthy at MIT in 1958, was one of the first to require dynamic memory allocation (and garbage collection to reclaim it), since LISP programs constantly create and discard linked list structures. The C language's malloc() and free() functions, standardized in the 1970s, gave programmers direct control over heap allocation, along with the responsibility to avoid memory leaks and use-after-free bugs. Heap management remains one of the hardest problems in systems programming. Fragmentation, where free memory becomes scattered into unusable small chunks, is a persistent challenge. Modern languages take different approaches: Java and Go use garbage collectors, Rust uses compile-time ownership rules, and C++ offers smart pointers that automate deallocation.
Context: Early programming language design, 1950s-1960s
Fun fact: The heap data structure (a tree-based priority queue) and heap memory (a dynamic allocation region) are completely unrelated concepts that unfortunately share the same name, confusing generations of computer science students.