#include <stdio.h>
void function()
{
int stackVar = 10;
printf("Stack variable = %d\n", stackVar);
}
int main(void)
{
function();
return 0;
}
What happens to the stack frame of function
when it returns?
#include <stdio.h>
void function()
{
int stackVar = 10;
printf("Stack variable = %d\n", stackVar);
}
int main(void)
{
function();
return 0;
}
What happens to the stack frame of function
when it returns?
This is undefined behaviour (as opposed to implementation-defined or unspecified). This means that the program is free to misbehave, or not, in any way is pleases.
This is spelled out in 6.2.4 Storage durations of objects:
1 An object has a storage duration that determines its lifetime. There are three storage durations: static, automatic, and allocated. Allocated storage is described in 7.20.3.
2 The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to reaches the end of its lifetime.
3 An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.
4 An object whose identifier is declared with no linkage and without the storage-class specifier static has automatic storage duration.
5 For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way. (Entering an enclosed block or calling a function suspends, but does not end, execution of the current block.) If the block is entered recursively, a new instance of the object is created each time. The initial value of the object is indeterminate. If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block; otherwise, the value becomes indeterminate each time the declaration is reached.
Firstly, you've edited the question dramatically, so other answers are (somewhat unfairly) no longer relevant. Still, to answer the current question:
What happens to the stack frame of function when it returns?
It seems to me you lack a general feel for how the stack operates. So - going a bit crazy here - but will try an analogy that might make it "click". You can imagine the stack frame as being like waves on the beach. The more deeply nested function calls get, and the more data those functions have in parameters and local variables, the more memory is in use. That's like waves reaching further up the beach. As scopes exit the memory is effectively released - the use to which that memory was put is forgotten. So too do waves recede. Still, throughout the lifetime of the program as different sequences of functions enter and exit, the same memory (level of the beach) is reused (under water) and forgotten (not under water). The bits furthest up the beach tend to be covered least often and for short durations, while some stays underwater until the weakest point of low tide... similarly things like recursive functions that aren't tail-recursion optimised can use a lot of memory briefly, but the stack variables created directly in main()
stay there until program termination.
You are invoking undefined behavior.
When you return from the function, the stack frame is destroyed (goes out of scope) and it might be you receive the value you left in the function, but this would be a coincidence.
undefined behavior. you are returning local variable address from the function, because the stack-frame is destroyed (out of scope) . Now if memory (address )is not overwritten then you will get same value else you will get garbage .