C尾调用优化 [英] C tail call optimization

查看:33
本文介绍了C尾调用优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常听到人们说 C 不执行尾调用消除.即使标准不保证它,它在实践中不是通过任何体面的实现来执行的吗?假设您只针对成熟的、实现良好的编译器,并且不关心为晦涩的平台编写的原始编译器的绝对最大可移植性,那么依赖 C 中的尾调用消除是否合理?

I often hear people say that C doesn't perform tail call elimination. Even though it's not guaranteed by the standard, isn't it performed in practice by any decent implementation anyhow? Assuming you're only targeting mature, well implemented compilers and don't care about absolute maximum portability to primitive compilers written for obscure platforms, is it reasonable to rely on tail call elimination in C?

此外,将尾调用优化排除在标准之外的理由是什么?

Also, what was the rationale for leaving tail call optimization out of the standard?

推荐答案

像C 不执行尾调用消除"这样的语句毫无意义.正如您自己正确指出的那样,这样的事情完全取决于实现.是的,任何体面的实现都可以轻松地将尾递归转换为 [相当于] 循环.当然,C 编译器通常不会对每个特定代码段中会进行哪些优化以及不会进行哪些优化做出任何保证.您必须编译它并亲自查看.

Statements like "C doesn't perform tail call elimination" make no sense. As you correctly noted yourself, things like this depend entirely on the implementation. And yes, any decent implementation can easily turn tail-recursion into [an equivalent of] a cycle. Of course, C compilers do not normally give any guarantees about what optimizations will and what optimizations will not happen in each particular piece of code. You have to compile it and see for yourself.

这篇关于C尾调用优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆