首页 文章

溢出主堆栈时有四种不同的结果

提问于
浏览
12

出于好奇,我正在玩这个代码溢出堆栈:

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演出,我在一台没有那么多RAM的笔记本电脑上 .

有什么想法可以在这里发生什么?

编辑:

我正在玩数组的大小(我认为这可能是导致不同错误的原因,但为什么?),还有一个:

4)具有完美的感觉 .

error: the type `[i32; 300000000000000]` is too big for the current architecture

我的系统架构是 x86_64 .

1 回答

  • 1

    看来上面的随机性与我的机器有关 .

    我在另一台机器上检查了相同的代码,它具有相同的 rustc 版本, ubuntu 版本和相同的架构 . 而且我的结果更可预测:

    如果数组的大小 536870871 或更大(没有得到案例4)我得到:

    Illegal instruction (core dumped)
    

    如果数组的大小是 536870870 或更小(不小到实际工作)我得到:

    thread '<main>' has overflowed its stack
    Illegal instruction (core dumped)
    

    我没有一次得到一个垃圾回收的情况 3) .

相关问题