What is the difference between stack and heap?

Asked 07-Dec-2019
Viewed 952 times

1 Answer


0

Stack is a special area of the memory of the computer which is used to store temporary variables created by a function. In stack, the variables are declared, stored and initialized during runtime. On the other hand, heap is a memory used by programming languages to store global variables. By default, all global variables are stored in heap memory space.
Stack is a linear data structure whereas a heap is a hierarchical data structure. Stack cannot be resized but a heap can be resized. In stack, memory is allocated in a contiguous block only whereas in a heap, memory is allocated in any random order. The access time of stack is faster while that of the heap is slower. Stack comprises of local variables only but heap allows us to access variables globally. Stack allocation and deallocation is done automatically by compiler instructions whereas the same thing needs to be done manually by a programmer in case of a heap.
What is the difference between stack and heap?
Stack image
What is the difference between stack and heap?
Heap image