So why would we even consider using heap?
The stack region has a predefined size, and it is usually pretty small,
about 2 MB or so.
On the other hand, heap does not have a predefined size,
and it is usually substantially larger than the stack.
In some compiler implementations,
stack and heap share the same place in memory, so the heap in that case
contains all of the memory which was not taken by the stack.
So if the heap is so much bigger, why would we even use stack?
Well,
first of all, stack provides you with the scope so that you
don't have to de allocate memory manually.
The other reason is the speed. Allocating memory in the stack is so much faster.
Just like we learned, basically all that stack needs to do to
allocate or deallocate memory is to move the stack pointer, which
is usually just one CPU instruction. Since all of the variables
are stacked next to each other,
there is no need for any additional verification. And
once the variable goes out of scope,
CPU will pop it out the stack by moving the stack pointer back.
That doesn't seem like a lot of work.
However, when you allocate memory on the heap,
your program actually has to work with the operating system to
find the sufficient amount of memory. Allocated memory on the
heap is not stacked together.
It actually looks like a heap.
The allocations happen all over the place, so the operating system has to
first find the sufficient amount of memory which is not already allocated
and then reserve it and return the memory address. After that, the program
needs to remember which parts of the heap are already allocated and the
size of these allocations. To do that, most implementations use the
so‑called free list.
This list basically keeps track of the unallocated memory on the heap.
Since a lot of management and bookkeeping is happening in the
background and the operating system needs to be involved,
heap allocations are slower and the allocation time is not consistent.
So if the heap is so slow,
why would we even use it? After all, 2 MB of data from the stack
is more than enough for some programs. And yes,
it's enough for some programs, but in this day and age we need more memory.
So if you need to allocate a large amount of memory in your program,
use heap; otherwise, use stack whenever possible.
If you're building your own data structures,
like, for example, a linked list, the heap may be useful because you are
free to allocate and deallocate nodes from the list whenever you need to,
and they are also not dependent on any scope.
This is why a lot of container classes from the
Standard Template Library use heap.
So we actually need heap to be able to access that functionality.
Of course, when you use those classes,
the heap allocation is abstracted away, and we will learn about
those abstractions in the following modules.
Another big advantage of the heap is the ability to allocate memory at runtime.
This is known as dynamic memory allocation,
and when someone mentions that the memory is dynamically allocated,
they usually refer to the heap.
To explain this, I will use variable length arrays,
but since we didn't talk about arrays yet,
we will have to cover this use case in the next module.