简体   繁体   中英

Four different outcomes when overflowing main stack

Out of curiosity, I was playing with overflowing the the stack with this code:

fn main() {
    let my_array: [i32; 3000000000] = [3; 3000000000];
    println!("{}", my_array[0]);
}

And to my surprise I ended with three different outcomes:

1) This is what I expected:

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

2) Surprisingly vague:

Illegal instruction (core dumped)

3) Totally puzzling:

208333333

In order for stochastic nature to show up I had to restart the shell, otherwise results were deterministic ( I would get the same error message over and over).

I compiled with just:

rustc my_file.rs

and excuted with:

./my_file

My rustc version:

rustc 1.0.0 (a59de37e9 2015-05-13) (built 2015-05-14)

My ubuntu version:

Distributor ID: Ubuntu
Description:    Ubuntu 14.04 LTS
Release:    14.04
Codename:   trusty

Also the size of the array I am trying to create is 12 gigs, I am on a tiny laptop that does not have that amount of RAM.

Any ideas what could be going on here?

Edit:

I was playing with the size of array (which I think might be the reason for different errors, but why?), and got one more:

4) Makes perfect sense.

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

and my system architecture is x86_64 .

It seems that above randomness is related to my machine.

I checked the same code on another machine, that has the same rustc version, ubuntu version and the same architecture. And my results a much more predictable:

If size of the array 536870871 or greater (without getting to case 4) I get:

Illegal instruction (core dumped)

If size of array is 536870870 or smaller (without being small enough to actually work) I get:

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

Not a single time have I gotten a case 3) where I had garbage returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM