【问题标题】:Stack overflow with large fixed size array in Rust 0.13Rust 0.13 中的大型固定大小数组的堆栈溢出
【发布时间】:2014-12-25 13:25:14
【问题描述】:

我希望向 Rust 专家验证这个简单的 Rust 程序(rustc 0.13.0-nightly 在 Linux x86-64 系统上):

/*
the runtime error is:
task '<main>' has overflowed its stack
Illegal instruction (core dumped)
*/

fn main() {
    let l = [0u, ..1_000_000u];
}

编译过程完美结束,没有错误,但在运行时程序失败并出现代码注释中显示的错误。

Rust 中固定大小数组的维度是否有限制,或者这是编译器中的某个错误?

【问题讨论】:

    标签: arrays stack-overflow rust


    【解决方案1】:

    Rust 的默认堆栈大小为 2MiB,你只是用完了堆栈空间:

    fn main() {
        println!("min_stack = {}", std::rt::min_stack());
    }
    

    要分配该大小的数组,您必须使用 box 在堆上分配它:

    fn main() {
        let l = box [0u, ..1_000_000u];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 2015-05-05
      • 1970-01-01
      • 2013-07-11
      • 2011-12-05
      • 2019-05-27
      • 2014-11-06
      • 2021-04-07
      相关资源
      最近更新 更多