common-lisp - 增加堆栈空间

标签 common-lisp sbcl

当我运行以下代码时:

(defun countdown (n)
  (if (>= n 0)
    (cons n (countdown (- n 1)))))

(countdown 100000)

我收到以下消息:

INFO: Control stack guard page unprotected
Control stack guard page temporarily disabled: proceed with caution

debugger invoked on a SB-KERNEL::CONTROL-STACK-EXHAUSTED in thread
#<THREAD "main thread" RUNNING {1002B03653}>:
  Control stack exhausted (no more space for function call frames).
This is probably due to heavily nested or infinitely recursive function
calls, or a tail call that SBCL cannot or has not optimized away.

PROCEED WITH CAUTION.

Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.

restarts (invokable by number or by possibly-abbreviated name):
  0: [ABORT] Exit debugger, returning to top level.

(SB-KERNEL::CONTROL-STACK-EXHAUSTED-ERROR)

如何增加堆栈空间?我不想更改上面的代码;我提供它纯粹是为了说明堆栈耗尽。

最佳答案

在命令行上使用 --help 选项调用 sbcl 会提供一些有用的常用选项,包括如何增加堆栈空间的选项。您想使用 --control-stack-size 参数。

$ sbcl --help
Usage: sbcl [runtime-options] [toplevel-options] [user-options]
Common runtime options:
  --help                     Print this message and exit.
  --version                  Print version information and exit.
  --core <filename>          Use the specified core file instead of the default.
  --dynamic-space-size <MiB> Size of reserved dynamic space in megabytes.
  --control-stack-size <MiB> Size of reserved control stack in megabytes.
...

通过在此处指定一个更大的值,我们可以运行您的代码:

$ sbcl --control-stack-size 100000
* (defun countdown (n)
  (if (>= n 0)
    (cons n (countdown (- n 1)))))
;=> (100000 … 6 5 4 3 2 1 0)

关于common-lisp - 增加堆栈空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19337763/

相关文章:

Lisp/参数传递

function - Lisp 匿名函数局部变量

installation - 如何使用Portacle IDE在Common Lisp中实现Common Music?

lisp:defun 的必需参数不是符号

post - 如何在 Hunchentoot 中获取 POST 作为函数的参数?

if-statement - 如何在 if 语句中使用 (or)?

LISP 局部/全局变量赋值

lisp:如何在范围内创建临时方法特化

performance - Lisp:衡量函数的性能

sockets - 检查 socket 是否仍然打开而没有阻塞