出于好奇,我在玩用这段代码溢出堆栈:
fn main() {
let my_array: [i32; 3000000000] = [3; 3000000000];
println!("{}", my_array[0]);
}
令我惊讶的是,我以三种不同的结果结束了:
1) 这是我的预期:
thread '<main>' has overflowed its stack
Illegal instruction (core dumped)
2) 令人惊讶的模糊:
Illegal instruction (core dumped)
3) 完全令人费解:
208333333
为了显示随机性,我必须重新启动 shell,否则结果是确定性的(我会一遍又一遍地收到相同的错误消息)。
我只编译了:
rustc my_file.rs
并执行:
./my_file
我的 rustc 版本:
rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)
我的ubuntu版本:
Distributor ID: Ubuntu
Description: Ubuntu 14.04 LTS
Release: 14.04
Codename: trusty
此外,我尝试创建的阵列大小为 12 gig,我使用的是一台没有那么多 RAM 的小型笔记本电脑。
有什么想法可以在这里发生吗?
编辑:
我在玩数组的大小(我认为这可能是导致不同错误的原因,但为什么?),又得到了一个:
4) 非常有意义。
error: the type `[i32; 300000000000000]` is too big for the current architecture
而我的系统架构是x86_64
。
最佳答案
上面的随机性好像和我的机器有关。
我在另一台机器上检查了相同的代码,它具有相同的 rustc
版本、ubuntu
版本和相同的架构。而且我的结果更可预测:
如果数组的大小 536870871
或更大(不涉及案例 4)我得到:
Illegal instruction (core dumped)
如果数组的大小是 536870870
或更小(没有小到足以实际工作)我得到:
thread '<main>' has overflowed its stack
Illegal instruction (core dumped)
我从来没有遇到过一个案例 3)
我有垃圾返回。
关于memory - 溢出主堆栈时的四种不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216602/