gpt4 book ai didi

rust - Rust-调试和 Release模式的堆栈大小是否不同?

转载 作者:行者123 更新时间:2023-12-03 11:34:44 25 4
gpt4 key购买 nike

考虑以下程序:

fn recursive_call(x: u32) -> u32 {
println!("x: {:?}", x);
recursive_call(x +1)
}

fn main() {
recursive_call(0);
}
当我运行 cargo build && ./target/debug/recursive_call时,它在 x: 58152之后崩溃:
x: 58152

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted (core dumped)
当我运行 cargo build --release && ./target/release/recursive_call时,这仅在 x: 104728之后崩溃:
x: 104728

thread 'main' has overflowed its stack
[...]
我想知道这种行为差异来自何处?版本与 Debug模式下的堆栈大小是否不同?还是这是因为我缺少一些编译时优化(如果是的话,那么优化会是什么?)

最佳答案

是的,在x86-64上的according to Godbolt在 Debug模式下为该函数保留的堆栈大小为120字节,而启用了优化功能(-O)则为72字节。那是很正常的。

关于rust - Rust-调试和 Release模式的堆栈大小是否不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65887272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com