Reputation: 4391
If I have the following code:
void bar(){
int x = 1;
foo();
}
void foo(){
while(true);
}
What happens to the memory int x
used in bar()
when foo()
is called? Is it freed? I know that the memory is freed if the function returns, however in this case the function simply never returns.
If I used this code, in which bar
calls foo
which, in turn, calls bar
and so on, would the program eventually run out of memory, or would the old instances of the functions be replaced by the new ones?
void bar(){
int x = 1;
foo();
}
void foo(){
int y = 1;
bar();
}
Upvotes: 2
Views: 2461
Reputation: 153498
What happens to the memory a function (
bar()
) uses when it calls another function (foo()
) ?
The memory will stay alive if bar()
may return and the memory is used after the call.
void foo();
void bar(){
int x = rand();
foo();
printf("%d\n", x);
}
Cases when the memory may or may not remain
// foo() is _Noretrun, no need to keep `x`
_Noretrun void foo();
void bar(){
int x = rand();
foo();
printf("%d\n", x);
}
// `x` optimized out - not needed after `foo()`
void foo();
void bar(){
int x = 1;
foo();
}
// Smart compiler knows `foo()` will not return.
void foo(){
while(true);
}
void bar(){
int x = rand();
foo();
printf("%d\n", x);
}
// optimized out
void bar(){
int x = 1;
foo();
printf("%d\n", x);// Could complies as `puts("1");`
}
Upvotes: 1
Reputation: 3527
What happens to the memory
int x
used inbar()
whenfoo()
is called? Is it freed? I know that the memory is freed if the function returns, however in this case the function simply never returns.
No, it's not freed. The x
variable is freed when bar returns, which never happens in your case.
If I used this code, in which
bar
callsfoo
which, in turn, callsbar
and so on, would the program eventually run out of memory, or would the old instances of the functions be replaced by the new ones?
You would eventually get stack overflow, because you never release the local variables (because you never return), and you only have a limited stack space available per thread. In windows using MSVC it's 1MB.
Upvotes: 2
Reputation: 169
In this case, x is allocated on the stack. When you call function foo(), all variables stored on the stack stay allocated - the new function uses a new stack 'frame'. This is required because if your function foo() ever were to return, then it would need to be able to access variable 'x' on the old stack frame. If that new function (foo) calls another, then both will still be consuming their stack memory. If foo recursively called foo, there would be one new 'foo' stack frame (and so, stack space allocated for variable y as well) for each recursive call. If there are too many function calls that consume too much stack space (recursively or not), you will eventually run out of stack space, and your program will fail.
Upvotes: 5
Reputation: 1847
Please read article http://www.geeksforgeeks.org/memory-layout-of-c-program/ to understand program layout.
To answer your question. all the function calls and local variable are saved on stack.
So when you call main, int x, they got pushed on stack and then you will push Foo function on stack along with int y.
Then you again push main and int x, so you will do it infinitely, and eventually you will receive an error saying stack overflow.
Upvotes: -2
Reputation: 29655
The memory is held on the stack and the stack pointer moves to make room for the local variables in main()
. In fact, the entire stack frame is preserved. If bar()
is called again then more memory will be locked on the stack. At some point you will have a stack overflow, not to be confused with http://www.stackoverflow/.
Upvotes: 1
Reputation: 2907
The end condition you describe is called a stack overflow, and yes, the program would eventually run out of memory in its stack segment. Each function, when called, is allocated a space on the stack that holds the data it needs to function called a stack frame.
void f1() {
int x;
f2();
}
void f2() {
int y;
f1();
}
Calling either of these functions would result in stack frames repeatedly being allocated on the stack until it exceeded the limit designated by the operating system.
See https://en.wikipedia.org/wiki/Call_stack
Upvotes: 2