Memory in a C/C++ program can either be allocated on stack or heap.
Prerequisite : Memory layout of C program.
Stack Allocation : The allocation happens on contiguous blocks of memory. We call it stack memory allocation because the allocation happens in function call stack. The size of memory to be allocated is known to compiler and whenever a function is called, its variables get memory allocated on the stack. And whenever the function call is over, the memory for the variables is deallocated. This all happens using some predefined routines in compiler. Programmer does not have to worry about memory allocation and deallocation of stack variables.
Heap Allocation : The memory is allocated during execution of instructions written by programmers. Note that the name heap has nothing to do with heap data structure. It is called heap because it is a pile of memory space available to programmers to allocated and de-allocate. If a programmer does not handle this memory well, memory leak can happen in the program.
Key Differences Between Stack and Heap Allocations
- In a stack, the allocation and deallocation is automatically done by whereas, in heap, it needs to be done by the programmer manually.
- Handling of Heap frame is costlier than handling of stack frame.
- Memory shortage problem is more likely to happen in stack whereas the main issue in heap memory is fragmentation.
- Stack frame access is easier than the heap frame as the stack have small region of memory and is cache friendly, but in case of heap frames which are dispersed throughout the memory so it cause more cache misses.
- Stack is not flexible, the memory size allotted cannot be changed whereas a heap is flexible, and the allotted memory can be altered.
- Accessing time of heap takes is more than a stack.
|Basic||Memory is allocated in a contiguous block.||Memory is allocated in any random order.|
|Allocation and Deallocation||Automatic by compiler instructions.||Manual by programmer.|
|Main Issue||Shortage of memory||Memory fragmentation|
|Locality of reference||Excellent||Adequate|
|Flexibility||Fixed size||Resizing is possible|
- Memory representation of Binomial Heap
- Heap overflow and Stack overflow
- Difference between Random Access Memory (RAM) and Content Addressable Memory (CAM)
- Difference between Virtual memory and Cache memory
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
- File Allocation Methods
- Difference between Byte Addressable Memory and Word Addressable Memory
- Computer Organization | Register Allocation
- Get the stack size and set the stack size of thread attribute in C
- Difference between Uniform Memory Access (UMA) and Non-uniform Memory Access (NUMA)
- Difference between Volatile Memory and Non-Volatile Memory
- K’th Least Element in a Min-Heap
- Minimum element in a max heap
- Maximum element in min heap
- K-th Greatest Element in a Max-Heap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ShubhamKatta