Runtime Environment Part 2 L11
Runtime Environment Part 2 L11
Part 2
Prepared By:
Dr. D. P. Singh
Graphic Era Deemed to be University, Dehradun
Runtime Memory Subdivision
• Program Code (Target Code) Program Code
Heap
Storage Allocation Strategies
1. Static Storage Allocation
• For any program if we create memory at compile time, memory
will be created in the static area. (allocates memory for variables
at compile time)
• Memory is created only once.
• It does not support dynamic data structure i.e memory is created
at compile time and deallocated after program completion.
• The drawback with static storage allocation is recursion is not
supported.
• Another drawback is size of data should be known at compile time
• Access of data is fast since address are known at compile time
e.g.
FORTRAN (designed to support static allocation)
Static Allocation cntd…
int i=5;
Code for main() Program Code
int proc(int j) Code for proc()
{
i (int) Global Static
int p;
Area
int q;
. Activation record for main() Stack
.
} Activation Record for proc()
int main()
{
int r; Heap
proc(r);
.
}
Storage Allocation Strategies cntd…
2. Stack Storage Allocation
int main()
{
// Memory for all these variables allocated onto the stack
int p;
int q[10];
int r = 20;
}
Storage Allocation Strategies cntd…
3. Heap Allocation
• Recursion is supported.
Heap Allocation cntd…
int main()
{
// The memory for 5 integers is allocated on heap.
int *ptr = new int[5];
}
Difference between Heap and Stack Storage
Allocation
Stack Storage Allocation Heap Storage Allocation
Memory shortage problem more likely to Less Likely (due to fragmented memory)
arise
Access to stack frame is easier (its cache Heap frames are dispersed throughout the
friendly) memory (more cache misses)
Thank You