至于自称函数的栈重用? [英] Regarding stack reuse of a function calling itself?
问题描述
如果,而在同一时间定义变量的函数调用自身
这将导致堆栈溢出?是否有任何的gcc选项来重复使用相同的协议栈。
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 ;
}
一个可以重复使用的功能堆栈帧它前面使用?
什么是GCC重用在尾递归相同的帧??选项
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??
推荐答案
是的。见
-foptimize同胞通话
-foptimize-sibling-calls
优化同属和尾递归调用。
Optimize sibling and tail recursive calls.
在各级启用-O2,-O3,-Os。
Enabled at levels -O2, -O3, -Os.
您函数编译为:
funcstack:
.LFB0:
.cfi_startproc
xorl %eax, %eax
jmp func
.cfi_endproc
(注意跳转到FUNC)
(note the jump to func)
重用堆栈帧时通过调用一个函数结束 - 这包括其全一般性操作堆栈把参数在正确的位置,并跳转到函数开始替换函数调用 - 是被称为著名的优化[I]尾调用去除[/ I]。它是由一些语言(例如计划)的递归调用规定(递归调用是前preSS自然的方式在这些语言中的循环)。正如上述,GCC对C实现的优化,但我不知道哪些其他编译器有它,我不会依赖于它的便携code。而且请注意,我不知道哪些限制有它 - 我不知道的实例gcc会操作堆栈如果参数类型是不同的。
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.
这篇关于至于自称函数的栈重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!