0% found this document useful (0 votes)
40 views10 pages

Runtime Environment Part 2 L11

Compiler design

Uploaded by

Addition Add
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views10 pages

Runtime Environment Part 2 L11

Compiler design

Uploaded by

Addition Add
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Runtime Environment

Part 2
Prepared By:
Dr. D. P. Singh
Graphic Era Deemed to be University, Dehradun
Runtime Memory Subdivision
• Program Code (Target Code) Program Code

• Static Data Objects


Global Static
• Dynamic Data Objects– Heap Area

• Automatic Data Objects -- Stack Stack

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

• Storage is organized as a stack and activation records are pushed


and popped as activation begin and end respectively.

• Locals and parameters are contained in the activation records

• Locals are contained in activation records so they are bound to


fresh storage in each activation.

• Recursion is supported in stack allocation (need to hold multiple


activation records for same procedure)
• Dynamic Memory Allocation is allowed
• Pointers to data locations are allowed
e.g. PASCAL, C
Stack Storage Allocation cntd…

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

• Memory allocation and deallocation can be done at any


time and at any place depending on the requirement of
the user.

• Heap allocation is used to dynamically allocate memory


to the variables and claim it back when the variables are
no more required.

• 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

The allocation and deallocation is done Needs to be done by the programmer


automatically manually.

Stack frame handling is easier Handling Heap frame is costlier

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

You might also like