Heap vs Stack

Is a Heap a FIFO?

No, a heap is not a FIFO. Heap memory has no fixed order, and a binary heap returns items by priority. Here is how the two differ, with C++ examples.

On this page
  1. A heap is not a FIFO
  2. The word heap has two meanings
  3. What FIFO means
  4. A binary heap returns items by priority
  5. Heap memory has no FIFO order
  6. The stack is last in, first out
  7. Summary

A heap is not a FIFO

No. FIFO stands for first in, first out. It describes a queue, where the item added first is the first one removed. A heap does not work that way. Heap memory has no fixed order, and a binary heap always returns its highest-priority item, not the one that arrived first.

The word heap has two meanings

The word “heap” appears in two places in programming:

  • Heap memory is the area your program uses for new and malloc.
  • A heap is a data structure, usually a binary heap. It is a tree-shaped structure where the top item is the largest (a max-heap) or the smallest (a min-heap).

Neither one is a FIFO. The memory heap has no ordering rule, and the data structure orders items by priority.

What FIFO means

In a FIFO queue, the first item in is the first item out. The standard library’s std::queue works this way.

#include <iostream>
#include <queue>

int main()
{
    std::queue<int> line;
    line.push(1);                       // first in
    line.push(2);
    line.push(3);                       // last in

    while (!line.empty())
    {
        std::cout << line.front() << " ";   // prints 1 2 3
        line.pop();                         // first in, first out
    }
    std::cout << "\n";
    return 0;
}

A binary heap returns items by priority

std::priority_queue is built on heap operations, and by default it is a max-heap: the largest item sits at the top.

#include <iostream>
#include <queue>

int main()
{
    std::priority_queue<int> tasks;     // max-heap by default
    tasks.push(1);
    tasks.push(3);
    tasks.push(2);

    while (!tasks.empty())
    {
        std::cout << tasks.top() << " ";   // prints 3 2 1
        tasks.pop();
    }
    std::cout << "\n";
    return 0;
}

The output follows priority, not the order of insertion. The 3 comes out first, even though the 1 went in first.

Heap memory has no FIFO order

Heap memory is handed out in blocks of whatever size the program asks for. Blocks can be freed in any order, and the allocator decides where each new request goes. Nothing in the language says that the oldest block is freed or reused first.

int main()
{
    int* a = new int(1);
    int* b = new int(2);
    int* c = new int(3);

    delete b;    // freed first, even though it was allocated second
    delete a;
    delete c;
    return 0;
}

Freeing b first is neither first in, first out nor last in, first out. The allocator may reuse the space from b for a later request, or it may not. That is an implementation detail, so your code must not depend on it.

The stack is last in, first out

The stack follows the opposite rule. The most recent function call is the first to return, so its local variables are removed first. Heap memory has neither order: it is not first in, first out, and it is not last in, first out.

For a fuller comparison of the two memory areas, read about c heap vs stack.

Summary

  • A heap in the memory sense is not a FIFO.
  • A binary heap, as a data structure, returns items by priority rather than by arrival order.
  • If you need first-in, first-out behavior, use a queue such as std::queue.

The complete guide to heap vs stack

Heap vs Stack Memory in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

The sizeof Operator in C++

Learn how the sizeof operator in C++ works: what it returns in bytes, how it handles arrays, pointers and structs, and common mistakes to avoid.

sizeof 6 min read