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

  • Thread starter AllenHe
  • Start date
  • Tags
    Code
In summary: However, if the function does take parameters, and they're not declared at the top of the function, they'll be pushed onto the stack when the function is called.
  • #1
AllenHe
74
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
  • #2


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


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.
 
  • #4


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


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

int main() {
	int z;
	f((int)&z); 
}
 
  • #6


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:
  • #7


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.
 

What does the code "void F()" mean?

The code "void F()" is a function declaration. It declares a function named "F" that does not return a value.

What is the purpose of declaring a function?

Declaring a function allows you to define a block of code that can be called and executed multiple times. This can help to make your code more organized and efficient.

Can the function "F" be called from anywhere in the code?

Yes, as long as the function has been declared before it is called, it can be called from anywhere in the code.

What happens if the function "F" is not declared before it is called?

If the function "F" is not declared before it is called, the code will not compile and an error will be thrown.

Can a function declaration include parameters?

Yes, a function declaration can include parameters that are used as inputs for the function. For example, "void F(int x)" would declare a function named "F" that takes an integer as an input.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
917
  • Programming and Computer Science
Replies
5
Views
812
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
627
  • Programming and Computer Science
2
Replies
57
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
684
  • Programming and Computer Science
Replies
34
Views
2K
Back
Top