The stack allocation is a runtime storage management technique. The activation records are pushed and popped as activations begin and end respectively.
Storage for the locals in each call of the procedure is contained in the activation record for that call. Thus, locals are bound to fresh storage in each activation, because a new activation record is pushed onto the stack when the call is made.
It can be determined the size of the variables at a run time & hence local variables can have different storage locations & different values during various activations. Suppose that the registered top marks the top of the stack. At runtime, an activation record can be allocated and deallocated by incrementing and decrementing top, respectively, by the size of the record.
If the procedure q has an activation record of size a then the top is incremented by before the target code of q is executed. When the control returns from q, the top of the stack are decremented by a.
The memory organization for the C program on the UNIX platform is as follows −
In C, data can be global, meaning it is allocated static storage and available to any procedure, or local, meaning it can be accessed only by the procedure in which it is discarded. A program consists of a list of global data declarations and procedures in which it is declared.
There are two pointers as one is stack pointer (SP) always points to a particular position in the activation record for the currently activate procedure. The second pointer is called top, which always points to the top of the stack i.e., top of the activation record.
The temporaries are used for expression evaluation and allocated above the activation record. An Activation Record is a data structure that is activated/ created when a procedure/function is invoked, and it contains the following information about the function.
Activation Record in 'C' language consist of
Old SP stores the value of stack pointer of Activation Record of procedure which has called this procedure which leads to the generation of this Activation Record, i.e., It is a pointer to the activation record of the caller.
Optional access link− It defines non-local data held in another activation record.
Optional control link− It points to the activation record of the caller.
Actual Parameter− It is used by the calling procedure to supply parameters to the called procedure.
Return value− This field is used by the called procedure to return a value to the calling procedure. The size of each of the above fields is determined at the time when the procedure is called. The size of almost all the fields can be determined at compilation time.