When run the code below:[code]void F(){

  • Thread starter Thread starter AllenHe
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion centers on the concept of recursion and stack overflow in programming. The code provided demonstrates a recursive function, F, that calls itself indefinitely, leading to a stack overflow error. The explanation clarifies that each call to F pushes a return address onto the stack, and this process continues until the stack space is exhausted. The importance of having a base case in recursive functions to prevent such overflows is emphasized. Additionally, the conversation touches on the mechanics of function calls in x86 assembly, noting that the "call" instruction pushes the return address onto the stack, contributing to the overflow when recursion is uncontrolled. The role of parameters in consuming stack memory is also mentioned, although the specific example does not utilize parameters.
AllenHe
Messages
74
Reaction score
0
When run the code below:
Code:
void F()

{

  F();

}
EStackOverFlow will appear.
The book says that there is a loop going on. Can anyone explain to me?
Because I think in the Void F function, it will only use itself once.
 
Last edited by a moderator:
Technology news on Phys.org


Yes, F will call again F and it will never stop. It will do this forever.
 


The first time F is called, right away there is a call to F. In the executable, the compiler has generated the code that is necessary to call a function. This code includes placing the return value on the stack and any parameters, and so on.

As soon as F is entered, there's a call to F again, with more stuff being pushed on the stack.

This process keeps repeating until you eventually run out of stack space, at which time the exception is thrown.
 


When ever you are using recursion make sure you have a base case to avoid stack overflows.
 


Code:
void f(int a){
	printf("%d\n", a-(int)&a);
	f(a);
}

int main() {
	int z;
	f((int)&z); 
}
 


Mark44 said:
The first time F is called, right away there is a call to F. In the executable, the compiler has generated the code that is necessary to call a function. This code includes placing the return value on the stack and any parameters, and so on.
The return value is returned in eax (in cdecl and stdcall). But I'm just nit picking. I'm sure you knew that, but were being general. :)
[/quote]Right. I meant return address, but wrote return value instead.
TylerH said:
In this case, it's the return address that's causing the overflow. The x86 assembly instruction "call" is used to call functions. When call is used, it pushes the return address onto the stack. In your program, the return address is pushed repeatedly by call until you get a stack overflow.
 
Last edited by a moderator:


TylerH said:
The return value is returned in eax (in cdecl and stdcall). But I'm just nit picking. I'm sure you knew that, but were being general. :)
Right. I meant return address, but wrote return value instead.
TylerH said:
In this case, it's the return address that's causing the overflow. The x86 assembly instruction "call" is used to call functions. When call is used, it pushes the return address onto the stack. In your program, the return address is pushed repeatedly by call until you get a stack overflow.
And if there are parameters to the function, they get pushed onto the stack as well, for some of the calling conventions, and that tends to eat up stack memory. I didn't bring this up, since the OP's function didn't take parameters.
 
Back
Top