尾递归支持可以缩短其他函数调用的堆栈吗? [英] Can tail recursion support shorten the stack for other function calls?
本文介绍了尾递归支持可以缩短其他函数调用的堆栈吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
支持尾递归的语言能否将相同的技术应用于非递归函数调用?
Can languages that support tail recursion apply the same technique to non-recursive function calls?
例如,如果函数 foo
做的最后一件事是返回调用 bar
的值,那么语言可能会丢弃 foo
的栈帧?是否有任何已知的语言可以实际执行此操作?
For example, if the last thing that function foo
does is return the value of calling bar
, might the language discard foo
's stack frame? Are any languages known to actually do this?
推荐答案
是的,可以.例如,考虑以下 C 代码:
Yes it can. For example, consider the following C code:
int f();
int g() { return f(); }
当我在 x86-64
上使用 gcc 4.6.3
和 -O3
编译它时,我得到了 的以下程序集g()
:
When I compile this using gcc 4.6.3
with -O3
on x86-64
, I get the following assembly for g()
:
g:
xorl %eax, %eax
jmp f ; <==== unconditional jump to f
这篇关于尾递归支持可以缩短其他函数调用的堆栈吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文