c# - 在 SICP 中,它表示即使递归调用实际上是迭代的,C 中的内存消耗也会增加。为什么?

标签 c# java c recursion sicp

原话是

One reason that the distinction between process and procedure may be confusing is that most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose ``looping constructs'' such as do, repeat, until, for, and while.

我对C语言不熟悉,那Java或者C#呢?他们也是这样吗? 为什么?

注:我还以为作者说的是不同语言的能力。但实际上只是编译器的不同实现而已。

最佳答案

C(和类似语言)通常使用 call stack存储每个函数的局部变量。每次调用函数时,都会占用更多的堆栈;每次函数返回时,堆栈使用量都会减少。

但是,智能编译器没有理由不能做像 tail-call optimisation 这样的事情。在适当的地方,从而消除了堆栈的使用。在像 Lisp 这样的语言中,我认为解释器需要来执行这样的优化,所以这可能是作者试图做出的区分。

关于c# - 在 SICP 中,它表示即使递归调用实际上是迭代的,C 中的内存消耗也会增加。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11593607/

相关文章:

C - 将用户输入存储在动态字符数组中?

c# - Uri ToString() 方法解码 Uri 查询

Java:如何创建按字符串路径排序的 Java 树

c# - 为什么不能在 Nullable<T> 简写上调用静态方法?

java - 如何解决 "import org.eclipse.wb.swt.SWTResourceManager"

java - 如何正确实现 Google Play 结算库

c - 如何用C语言替换txt文件中的单词?

c - 将ASM转换为C

javascript - 为什么我的 asp 文件上传控件文本框是可点击的?

C# WPF MVVM Caliburm-micro 动态绑定(bind) `GradientStops`