views:

106

answers:

6

if a function calls itself while defining variables at the same time would it result in stack overflow? Is there any option in gcc to reuse the same stack.

void funcnew(void)
{
   int a=10;
   int b=20;
   funcnew();
   return ;
 }

can a function reuse the stack-frame which it used earlier? What is the option in gcc to reuse the same frame in tail recursion??

A: 

No, each recursion is a new stack frame. If the recursion is infinitely deep, then the stack needed is also infinite, so you get a stack overflow.

John Knoeller
A: 

No, this isn't possible. And for a good reason. Separate stack frames make programming much easier and safer.

But why would you want it? The recursive call has no stop-condition anyway and will hang your computer.

Eli Bendersky
+3  A: 

Even without defining the parameters you'd get a stackoverflow. Since the return address also is pushed onto the stack.

It is (I've learned this recently) possible that the compiler optimizes your loop into a tail recursion (which makes the stack not grow at all). Link to tail recursion question on SO

Toad
Tail recursion does not apply here; to do tail-call-optzn, the call has to be the last thing the function does. To avoid an infinite loop, there must be some terminating case where the function returns w/o another recursive call. Neither of things cases apply to the code in the question.
Tim Schaeffer
@tim: The recursive call IS the last thing the function does. The return is a statement which has no effect and can be left out. I agree that the function doesn't terminate and it would cause an infinite loop. However it would not cause a stackoverflow if it was optimized for tail recursion by the compiler.
Toad
+1  A: 

Yes. See

-foptimize-sibling-calls

Optimize sibling and tail recursive calls.

Enabled at levels -O2, -O3, -Os.

Your function is compiled to:

funcstack:
.LFB0:
    .cfi_startproc
    xorl    %eax, %eax
    jmp func
    .cfi_endproc

(note the jump to func)

Reusing the stack frame when a function end by a call -- this include in its full generality manipulating the stack to put the parameters at the correct place and replacing the function call by a jump to the start of the function -- is a well known optimisation called [i]tail call removal[/i]. It is mandated by some languages (scheme for instance) for recursive calls (a recursive call is the natural way to express a loop in these languages). As given above, gcc has the optimisation implemented for C, but I'm not sure which other compiler has it, I would not depend on it for portable code. And note that I don't know which restriction there are on it -- I'm not sure for instance that gcc will manipulate the stack if the parameters types are different.

AProgrammer
+1  A: 

Yes, in some cases the compiler may be able to perform something called tail call optimization. You should check with your compiler manual. (AProgrammer seems to have quoted the GCC manual in his answer.)

This is an essential optimization when implementing for example functional languages, where such code occurs frequently.

Hans W
A: 

You can;t do away with the stack frame altogether, as it is needed for the return address. unless you are using tail-recursion, and your compiler has optimised it to a loop. But to be completely technically honest, you can do away with all the variables in the the frame by making them static. However, this is almost certainly not what you want to do, and you should not do it without knowing exactly what you are doing, which as you had to ask this question, you don't.

anon