简体   繁体   English

为什么 Rust 中的堆栈值相差如此之远?

[英]Why are stack values so far apart in Rust?

When I run当我跑

fn main() {
    let x: i32 = 0;
    println!("{:p}", &x);
    let y: i32 = 1;
    println!("{:p}", &y);
}

in the Rust playground , the values printed are, in decimal, 88 apart.Rust 操场中,打印的值以十进制表示,相隔 88。 My expectation would be that they would be 4 or 8 (bytes) apart.我的期望是它们相隔 4 或 8(字节)。 Why is it so large?为什么这么大?

The println! println! macro will use stack variables too.宏也将使用堆栈变量。 If you swap the order of your statements around (in Rust Playground debug at least), the two pointers are 4 bytes apart:如果您交换语句的顺序(至少在 Rust Playground 调试中),两个指针相隔 4 个字节:

fn main() {
    let x: i32 = 0;
    let y: i32 = 1;
    println!("{:p}", &x); // 0x7ffe0b865db0
    println!("{:p}", &y); // 0x7ffe0b865db4
}

There are no guarantees about how the stack is used, and it's very likely to be different with an optimised binary.无法保证堆栈的使用方式,并且很可能与优化的二进制文件不同。

After a small test and some reading ( println is a macro) i would assume that the macro creates additional code that causes the addresses of your variables to be more than the expected 4-8 bytes appart.经过一个小测试和一些阅读println是一个宏),我会假设宏创建了额外的代码,导致变量的地址超过预期的 4-8 个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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