Stacks and heaps are classical CS concepts especially in data structures and algorithms. In algorithms, a stack is a LIFO (Last In, First Out) structure while heap is a tree-based structure with strict ordering sub-divided into min-heap and max-heap and is used for priority queues. However, there usage in program memory is not analogous to there usage in data structures. In fact, they are nothing but namesakes.
Stack and heap memory are distinct memory regions for program execution. The names were given for historical reasons, stack for example because of LIFO and heap because its plenty and globally accessible.
Stack Memory
Stack is fast, fixed size, LIFO memory used for local variables and stack frame. Stack frame contains details of function call like parameters, local variables, return address, saved frame (caller’s frame). The stack memory is fixed size and can be depleted and further attempts to store there will result in the infamous stack overflow. Stack overflow is a situation where a program runs out of stack memory and crashes. Generally, a stack is used to maintain progression of a program.
Heap Memory
Heap is slower, dynamically managed region in a program memory used to store global variables and objects that persists beyond a functions’ scope. There is a debate on whether heap is synonymous to freestore, this is because, C++standards avoids OS-specific terminology and freestore is then defined behaviorally. Heap is generally realized through Direct Memory Access (DMA) which entails explicit request through pointers or implicitly through object creation.
Differences
| Basis | Explanation |
|---|---|
| Access |
Stack memory are local to a thread Heap is shared among threads i..e contains global variables and objects |
| Management |
Stack memory is automatically managed by the compiler. Heap memory managed by the programmer or garbage collector. |
| Speed |
Stack memory is faster because it involves moving a single pointer because its a FIFO. Heap memory is slower because it involves searching for free space. |
| Size |
Stack has limited size dictated by the OS. There is always space for heap memory provided adequate fragment of memory exists. |
| Structure |
Stack memory is contiguous block of memory. Heap memory is non-contiguous fragments. |
| Persistence |
Stack is automatically destroyed when a function returns or block executed. Heap remains until de-allocated by the programmer/ garbage collector. |
| Usage |
Stack stores local variables and function call information. Heap is for large, dynamic or shared data structures. |
| Adapt |
Stack is fixed and “overflows” when full. Heap grows safely as needed. |
YOU MIGHT ALSO LIKE
In critical or safety systems, usage of heap is sometimes completely avoided. This is because of these problems related to heap memory:
1. Dangling pointers
When a pointer exists after its usage is over and the memory it pointed to deleted, it retains the memory address unless explicitly reassigned with nullptr. These pointers pointing to non-existent variables indefinitely are called dangling pointers.
2. Memory fragmentation
Memory fragmentation occurs when a objects and variables of different types are assigned adequate space on request, there will be small useless sections which may have been useful if were compact/contiguous.
3. Multiple deallocations
Occur when you deallocate an already deallocated (and hence dangling) pointer for a second time using either delete or delete[] in C++, is another recipe for disaster.
4. Memory Leaks
Memory leak is a technical term used to describe continuous reduction of memory space due to old structures holding on to memory resources "forever". This is critical issue in older C++ or legacy systems using raw pointers, raw pointers require the user to explicitly manage memory. It's unbelievably easy to forget releasing memory. Consequently, there is progressive reduction in usable space.
5. Allocation/Deallocation mismatch
Both dynamic array and raw pointers are initialized using