![](/img/trans.png)
[英]Does the address of a pointer (&ptr) change when reassigned (*ptr = n)? Why doesn't C allow const pointer (const int**) to non-const (int**) pointer
[英]Why does a const disappear during linking when static doesn't?
我的 static 库箱中有这样的 function:
use super::*;
static BLANK_VEC: [u8; 16] = [0_u8; 16];
pub fn pad(name: &'static str) -> String {
let mut res = String::from(name);
res.push_str(&String::from_utf8_lossy(&BLANK_VEC[name.len()..]));
res
}
当我将它链接到 C 代码时,它按预期工作,但如果我链接下面的代码(唯一的区别是const
而不是static
),ZD304BA20E96D87411588_EEABAC850E34Z BLANK_VEC
文件中不会出现。 它可以编译和运行,直到它得到一个 HardFault。
use super::*;
const BLANK_VEC: [u8; 16] = [0_u8; 16];
pub fn pad(name: &'static str) -> String {
let mut res = String::from(name);
res.push_str(&String::from_utf8_lossy(&BLANK_VEC[name.len()..]));
res
}
这是 Rust 端的错误吗? 我认为是因为const
变量以某种方式超出了 scope 。 我可以引用它并编译它。 有保证的 memory 安全性在哪里? 为什么我不必使用unsafe
块来做到这一点?
如果这取决于我的 linker:我使用arm-gcc-none-eabi
。
编辑:我理解为什么会发生这种情况,但 Rust 不应该确保用户使用不会消失的变量吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.