Tuesday 29 April 2014

STACK Memory

Let's look at the following example and see how the local variables are set in the stack area:
void f1()
{
 int a;
 short b[4];
 double c;
 f2();
 f3();
} 
When we call f1() function, space needs to be set aside. The following picture shows activation records/stack frame:
locals 
When the function f1() is called, the stack pointer will be decremented by 20 bytes which is the size of the variables of f1().
stack_pointer 
void f2()
{
 int x;
 char *y;
 char *z[2];
 f3();
}

void f3()
{
 double m[3];
 int n;
}
The following diagram shows the movement of stack pointer, sp: f1()->f1.f2()->f2.f3()->f2.f3() exits->f1.f2() exits.
stack_pointer_f1_f2_f3
After the f1.f2(), when we call f1.f3(), the variables of the function f3() simply overwrites them onto the area where the variables of f2() were.

No comments:

Post a Comment