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

  • Thread starter Thread starter AllenHe
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around the behavior of recursive function calls in programming, specifically focusing on a code snippet that leads to a stack overflow. Participants explore the mechanics of recursion, the role of the call stack, and the importance of base cases in recursive functions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants assert that the function F calls itself indefinitely, leading to a stack overflow.
  • One participant explains that each call to F results in additional data being pushed onto the stack, eventually exhausting stack space.
  • Another participant emphasizes the necessity of a base case in recursive functions to prevent stack overflows.
  • A code example is provided that demonstrates a similar recursive behavior, prompting further discussion about stack usage.
  • There is a clarification regarding the terminology used, where one participant corrects themselves about the return address versus return value in the context of function calls.
  • Participants discuss the mechanics of the x86 assembly instruction "call" and its role in pushing return addresses onto the stack during recursive calls.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of recursion and the resulting stack overflow, but there are nuances in the explanations and terminology used. The discussion remains open with various perspectives on the implications of recursion.

Contextual Notes

Some participants note the importance of understanding calling conventions and how parameters affect stack memory usage, although the original function discussed does not take 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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 57 ·
2
Replies
57
Views
5K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K