In the stack, memory is reserved for main
which we call stack frame for the main
function.
When we call the Add
function, memory is reserved on top of the stack. In the Add
function stack frame, a
and b
are local pointers and c
is an integer which calculates the sum and then we return the reference. c
is a local variable of Add
function.
Now when the Add
function execution is completed the memory space in the stack is also deallocated so when we try to access this address in the main
with pointer p
, what we are trying to access is basically a deallocated space. The compiler gives a warning but why does it still print the value 5 correctly?
The answer to that may be that the machine didn't deallocate the memory space as it didn't deem it necessary as there were not any more functions. But if we write another function Hello
then it should definitely deallocate the space for Add
function in the call stack but the program still prints
Yay 5
Is it because like in heap we need to assign a pointer to null
after freeing it or else we can still access it? Is something like that related here?
/* void Hello()
{
printf("Yay");
} */
int* Add(int *a,int *b)
{
int c=*a+*b;
return &c;
}
int main()
{
int a=1,b=4;
int *p=Add(&a,&b);
// Hello();
printf("\t%d",*p);
return 0;
}
c
here?int c
? – CongruousHello()
function is smaller that theAdd()
function. – Biafra