Consider the C code:
int a = 2;
int main()
{
int x= func2(5);
printf(“%d”, x);
return 0;
}
int func1(int n)
{
int p;
p= a*n*n;
printf(“%d”, p);
return 3;
}
void func2(int n)
{
int k;
func1(n) ;
k=func1(2*n);
printf(“%d”,k);
return 7;
}
Now we have to picturize the whole scenario in HEAP and STACK memory.
Ans: We have no dynamically allocated variables in this program. Hence there is not much role of HEAP memory. We analyze the memory allocation and de-allocation in STACK on next page. We know stack memory is used to store local variables and the return address pointer, frame pointer (the pointer which is used to point to the starting of the frame just below) and other registers.
The global variable ‘a’ with value 2 is stored in the fixed memory and the memory remains allocated till the program execution ends.
Now as we enter into main, a frame is created in the stack. Next with the statement int x= func2(5), memory is reserved for the local variable x in the stack.
Now we have a function call to function func2 hence a new frame is created and firstly return address to main is stored in the stack. Next we have to allocate a space for variable n which is a parameter of the function func2() and 5 is stored. After this another local variable k is allocated space in stack with random value.
Now we make a function call to func1(). Hence a new frame is created in the stack and n=5 is passed as parameter. Firstly return address to the function func2() is saved on stack. Next space is allocated to variable n and given value 5. This variable n is different from the variable n in the function func2() although they have same names but different addresses. Now we have the statement int p; i.e. again memory is allocated for another local variable p and random value is there in the memory. After this the statement p=x*n*n evaluates tostore a value 2*5*5 = 50.
After this value of p is printed and value 3 gets returned. As the function func1 ends & returns to the address stored in the address register and hence its frame is de-allocated memory and memory map of stack is as:
Now we are in the function func2(). Next statement we have is k=func1(2*n); Hence a function call to func1 is made with parameter 2*5=10 and a new frame is created. In the func1 function firstly return address to func2 function is saved along with other registers. Next space is allocated to variable n and given value 10. This variable n is different from the variable n in the function func2() although they have same names but different addresses. Now we have the statement int p; i.e. again memory is allocated for another local variable p and random value is there in the memory. After this the statement p=x*n*n evaluates tostore a value 2*10*10 = 200.
Now value of p is printed and 3 gets returned and stored in variable k in function func2.
After this value of k gets printed and value 7 is returned. The function func2 returns to main and 7 gets stored in x.
After this value of x is printed and program ends. And hence the whole stack is de-allocated and even global variable ‘a’ is de-allocated.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.