Difference Between Stack and Heap
Difference Between Stack and Heap
techdifferences.com/difference-between-stack-and-heap.html
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.
Comparison Chart
Basis for
comparison Stack Heap
1/3
Basis for
comparison Stack Heap
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 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.
2/3
3. Implementation of a stack is complex. As against, implementation of a heap is
simple.
4. A function call in stack takes O(N) time. In contrast, it takes O(1) time in a heap.
5. Stack implementation mainly suffers from the memory shortage problem. On the
contrary, the main issue in a heap is fragmentation.
6. 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.
7. 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.
8. A heap takes more accessing time than a stack.
Conclusion
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.
3/3