Stack and Heap are the memory segments used in memory allocation techniques. The primary difference between Stack and heap is that stack involves linear and sequential allocation of the memory which is used in static memory allocation whereas heap acts as a pool of storage area that allocated the memory randomly (Dynamic memory allocation).
Speed is the major parameter which distinguishes stack and heap; a stack is significantly faster than a heap.
Content: Stack Vs Heap
|Basis for comparison||Stack||Heap|
|Basic||Memory is allocated in (LIFO) Last in first out fashion.||Memory is allocated in random order.|
|Allocation and Deallocation||Automatic||Manual|
|Issue||Shortage of memory||Memory fragmentation|
|Locality of reference||Excellent||Adequate|
|Flexibility||Fixed size and is not flexible||Resizing is possible|
Definition of Stack
Stack allocation follows a LIFO (Last in first out) strategy for assigning the memory to the processes with the help of push and pop operation. Each block in memory is of fixed size which cannot be expanded or contracted. The last entry in the stack is accessible at any moment. Stack uses a contiguous memory where a pointer named as stack base points to the first entry of the stack and another pointer named as the top of the stack points to the last entry of the stack.
Stack also support function calls. A function call can hold a collection of the stack entries, is known as the stack frame. Another name of the stack frame is activation record in the context of the compiler as it stores the data used at the time of program compilation. Whenever a function is called the stack frame is pushed into the stack.
A stack frame is comprised of either addresses or values of the function’s parameter and return address which signifies the where the control should be returned after completing function’s execution.
Definition of Heap
Heap allocation does not follow any definite approach; rather it allows random assignment and deassignment of the memory. An assignment request by a process gives back with a pointer to the allocated memory area in a heap, and the process accesses the allocated memory area through the pointer.
Deallocation is carried through the deallocation request, dissimilar to the stack where memory is deallocated automatically. Heap develops holes in the memory allocation when data structures are built and freed. It is used at the runtime.
Key Differences Between Stack and Heap
- In a stack, the allocation and deallocation is done by CPU and is automatic whereas, in heap, it needs to be done by the programmer manually.
- Heap frame handling is costlier than stack frame handling.
- Implementation of a stack is complex. As against, implementation of a heap is simple.
- A function call in stack takes O(N) time. In contrast, it takes O(1) time in a heap.
- Stack implementation mainly suffers from the memory shortage problem. On the contrary, the main issue in a heap is fragmentation.
- Access to a stack frame is easier than the heap as the stack is confined to the small region of memory and it always hit the cache, but heap frames are dispersed throughout the memory so the memory accessing can cause more cache misses.
- Stack is not flexible, the memory size allotted cannot be changed. On the other hand, a heap is flexible, and the allotted memory can be altered.
- A heap takes more accessing time than a stack.
Stack allocation is faster but complex. On the other hand, a heap is slower, but its implementation is simpler than a stack. Heap is more efficient than the stack.