[英]Why is a process's address space divided into four segments (text, data, stack and heap)?
[英]Four different outcomes when overflowing main stack
出於好奇,我正在玩這個代碼溢出堆棧:
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
。
看來上面的隨機性與我的機器有關。
我在另一台機器上檢查了相同的代碼,它具有相同的rustc
版本, ubuntu
版本和相同的架構。 而且我的結果更可預測:
如果數組536870871
或更大的大小(沒有得到案例4)我得到:
Illegal instruction (core dumped)
如果數組的大小是536870870
或更小(不小到實際工作)我得到:
thread '<main>' has overflowed its stack
Illegal instruction (core dumped)
沒有一次我得到了一個案例3)
我有垃圾返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.