DEPARTMENT OF CSG YEAR : IV BTECH I SEM SUBJECT : COMPILER DESIGN Seminar Presentation On TOPIC : STACK ALLOCATION OF SPACE (UNIT IV ) FACULTY: MRS.A.NANDHINI SREE PRESENTED BY: K. NIKHIL ( 22R91A7427) N. KARTHIK ( 23R95A7404)
INTRODUCTION Before learning about “ Stack Allocation of Space “ there is a need to know about Run Time Environment . What is Run Time Environment ? The CPU can only execute a program that is in the main memory. Therefore, after compilation, the compiler requests a block of memory from the operating system to store the compiled program. This entire process of managing memory at run-time is known as run-time storage management or run time environment. The main memory where the program resides during execution is divided into four main areas 1. Code Area 2. Static Data Area 3. Heap 4. Stack
CODE Static Data Area HEAP FREE SPACE STACK This is where the executable code of the program is stored. This area stores static and global variables The heap is used for dynamic memory allocation . The stack operates on a Last-In, First-Out (LIFO) principle and is used to store activation records when a function is called. LOW MEMORY ADDRESS HIGH MEMORY ADDRESS
Stack Allocation of Space The stack is the component of run-time management dedicated to handling the sequence of function calls. What it does? Every time a function is called, a new block of memory, called an activation record or stack frame , is pushed onto the top of the stack. How it works? It follows a strict Last-In, First-Out (LIFO) principle. When a function finishes, its stack frame is popped off the top, automatically freeing the memory. Key Characteristics Automatic: The compiler handles all the work of adding and removing from the stack. The programmer doesn't have to manually manage this memory. Fast: Allocating and deallocating memory on the stack is extremely fast. It's as simple as moving a single pointer to a new address. Temporary Storage: The data stored on the stack is temporary and exists only for the lifetime of the function call.
STRUCTURE OF A STACK FRAME OR ACTIVATION RECORD
Actual parameters: Holds the parameters passed to the function by the calling function. Returned values: Stores the value that the function returns to the caller. Control link: A pointer to the activation record of the function that made the call. Access link: Used to access the local data of other functions in the program. Saved machine status: Contains the address of the next instruction to be executed after the function finishes. Local data: Stores the local variables of the function. Temporaries: Holds temporary values that are generated during the evaluation of expressions.
Explanation with an Example :
Actual Parameters In the call calculate_area (l, w) , the values of l (which is 10) and w (which is 5) are passed. These values are copied into the Actual parameters section of the activation record so that calculate_area can use them as length and width . 2. Returned Values The calculate_area function computes area = 10 * 5 , which is 50 . The line return area; places the value 50 into this field. The main function then reads this value and assigns it to the result variable . 3. Control Link The activation record for calculate_area will contain a control link that points back to the activation record of the main function . When calculate_area is finished, the program uses this link to pop the current record off the stack and return control to main.
4. Saved Machine Status The system saves the address of the printf statement inside the main function. Once calculate_area returns its value, the program uses this saved address to know that it should continue executing from that printf line. 5. Local Data The variable int area; is local to the calculate_area function. Memory for area is allocated in this part of the activation record. 6. Temporaries In the expression area = length * width;, the result of length * width might be stored in a temporary, hidden variable before it is assigned to area. 7. Access Link The access link is used in languages that support nested functions (like Pascal or JavaScript) to access variables of the outer (parent) functions. C does not support nested functions, so this field is often not needed.
DRAWBACKS : Fixed Size: The stack has a limited, pre-determined size. If you call too many functions (like in infinite recursion) without returning, you will run out of space, which causes a famous error: a stack overflow . It is slower than static allocation. Reference to non – local variables can’t be retained.