Stack overflow with large fixed size array in Rust 0.13

淺唱寂寞╮ 提交于 2019-12-10 17:16:27

问题


I wish to verify with the Rust expert this simple Rust program (rustc 0.13.0-nightly on Linux x86-64 system):

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

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

The compile process ends perfectly with no error but at runtime the program failed with the error shown in the code comment.

Is there a limit to the dimension of fixed size array in Rust or is this a bug somewhere in the compiler?


回答1:


Rust has a default stack size of 2MiB, you are just running out of stack space:

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

To allocate the array of that size you have to allocate it on the heap using box:

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


来源:https://stackoverflow.com/questions/26637158/stack-overflow-with-large-fixed-size-array-in-rust-0-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!