Difference between Stack and Heap ?
Stack | Heap |
---|---|
Stack has very fast to access. | Heap has slower to access while compared to stack. |
Limit on stack size depends on OS. |
No limit on memory size. |
Stack variables cannot be resized. | Heap variables can be resized using realloc() method. |
In Stack,space is managed efficiently by CPU, memory will not become fragmented. |
There is no guaranteed efficient use of space, memory may become fragented over time as blocks of memory are allocated, then freed. |
Manage memory during allocating and freeing variables. | Don’t have to explicitly de-allocate variables. |
Stack to accessed only local variables. | Heap variables can be accessed globally. |
Stack memory is used only by one thread of execution. |
Heap memory is used by all the parts of the application |
The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. |
Element of the heap have no dependencies with each other and can always be accessed randomly at any time. |