Stack Vs Heap Memory Allocation: Memory Layout of C Program
Stack Vs Heap Memory Allocation: Memory Layout of C Program
Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java Python
stack memor y allocation because the allocation happens in the function call stack. The
called, its variables get memor y allocated on the stack. And whenever the function
call is over, the memor y for the variables is de-allocated. This all happens using some
predefined routines in the compiler. A programmer does not have to worr y about
method finishes its execution all the data belongs to that method flushes out from the
stack automatically. Means, any value stored in the stack memor y scheme is
accessible as long as the method hasn’t completed its execution and currently in
running state.
Key Points :
allocation because the data stored can only be access by owner thread.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
allocation.
you have read and understood our
Cookie Policy &
Privacy Policy
Stack-memor y has less storage space as compared to Heap-memor y.
Got It !
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ 1/9
28/11/2021, 09:55 Stack vs Heap Memory Allocation - GeeksforGeeks
CPP
int main()
{
// All these variables get memory
// allocated on stack
int a;
int b[10];
int n = 20;
int c[n];
}
Heap Allocation: The memor y is allocated during the execution of instructions written
by programmers. Note that the name heap has nothing to do with the heap data
always creates in Heap-space and the referencing information to these objects are
memor y allocation was because the data stored in this space is accessible or visible to
all threads. If a programmer does not handle this memor y well, a memor y leak can
The Heap-memor y allocation is fur ther divided into three categories :- These three
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
Young Generation – It ’s the por tion of the memor y where all the new data(objects)
you have read and understood our
Cookie Policy &
Privacy Policy
are made to allocate the space and whenever this memor y is completely filled then
Got It !
the rest of the data is stored in Garbage collection.
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ 2/9
28/11/2021, 09:55 Stack vs Heap Memory Allocation - GeeksforGeeks
Old or Tenured Generation – This is the par t of Heap-memor y that contains the
older data objects that are not in frequent use or not in use at all are placed.
Permanent Generation – This is the por tion of Heap-memor y that contains the
Key Points :
This memor y allocation scheme is different from the Stack-space allocation, here
to remove the old unused objects in order to use the memor y efficiently.
Stack-memor y.
program) runs.
CPP
int main()
{
// This memory for 10 integers
// is allocated on heap.
int *ptr = new int[10];
}
Intermixed example of both kind of memor y allocation Heap and Stack in java:
Java
class Emp {
int id;
String emp_name;
public Emp(int id, String emp_name) {
this.id = id;
this.emp_name
We use cookies to ensure you have the=best
emp_name;
browsing experience on our website. By using our site, you
acknowledge that
} you have read and understood our
Cookie Policy &
Privacy Policy
}
Got It !
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ 3/9
28/11/2021, 09:55 Stack vs Heap Memory Allocation - GeeksforGeeks
Following are the conclusions on which we’ll make after analyzing the above
example :
A s we star t execution of the have program, all the run-time classes are stored in
Then we find the main() method in the next line which is stored into the stack along
with all it ’s primitive(or local) and the reference variable Emp of type Emp_detail
will also be stored in the Stack and will point out to the corresponding object stored
in Heap memor y.
Then the next line will call to the parameterized constructor Emp(int, String) from
main( ) and it ’ll also allocate to the top of the same stack memor y block. This will
store:
The reference variable of String emp_name argument which will point to the
Then the main method will again call to the Emp_detail() static method, for which
allocation will be made in stack memor y block on top of the previous memor y
block.
So, for the newly created object Emp of type Emp_detail and all instance variables
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ 4/9
28/11/2021, 09:55 Stack vs Heap Memory Allocation - GeeksforGeeks
Fig.1
1. In a stack, the allocation and de-allocation are automatically done by the compiler
2. Handling of Heap frame is costlier than the handling of the stack frame.
3. Memor y shor tage problem is more likely to happen in stack whereas the main issue
4. Stack frame access is easier than the heap frame as the stack have a small region of
memor y and is cache-friendly, but in case of heap frames which are dispersed
5. A stack is not flexible, the memor y size allotted cannot be changed whereas a heap
Comparison Char t
Parameter STACK HE AP
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Allocation and Automatic by compiler Manual by the programmer.
De-allocation instructions.
Got It !
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ 5/9
28/11/2021, 09:55 Stack vs Heap Memory Allocation - GeeksforGeeks
Parameter STACK HE AP
reference
Safety Thread safe, data stored can only Not Thread safe, data stored
structure
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
133
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/
Lik 6/9
28/11/2021, 09:55 Stack vs Heap Memory Allocation - GeeksforGeeks
Like 133
Next
Ar ticle Contributed By :
Ankit_Bisht
@Ankit_Bisht
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
Vote for difficulty
you have read and understood our
Cookie Policy &
Privacy Policy
Current difficulty :
Easy
Got It !
Easy Normal Medium Hard Expert
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ 7/9
28/11/2021, 09:55 Stack vs Heap Memory Allocation - GeeksforGeeks
Easy Normal Medium Hard Expert
Load Comments
Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Copyright Policy Video Tutorials
JavaScript Videos
Bootstrap
@geeksforgeeks
, Some rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Got It !
https://www.geeksforgeeks.org/stack-vs-heap-memory-allocation/ 9/9