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 |
-local to a thread -shared among threads i..e contains global variables and objects |
| Management |
-automatically managed by the compiler -managed by the programmer or garbage collector |
| Speed |
-involves moving a single pointer because its a FIFO, faster -slower, searching for free space |
| Size |
Limited size dictated by the OS Provided adequate fragment of memory exists |
| Structure |
Contiguous block of memory non-contiguous fragments |
| Persistence |
-Automatically destroyed when a function returns or block executed. -Remains until de-allocated by the programmer/ garbage collector |
| Usage |
Stores local variables and function call information Large, dynamic or shared data structures |
| Adapt |
Fixed and “overflows” when full -Grows safely as needed |
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 deleted by the programmer or garbage collector. 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.