What is the difference between stack and heap?

0 votes
118 views
Shikhar Arora asked 07-Dec-2019 in Technology by Shikhar Arora

What is the difference between stack and heap

1 Answer

0 votes
Sangsaptak Pal answered 11 days ago by Sangsaptak Pal
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