在可执行文件的哪个段(.BSS,.DATA,其他)中存储了静态变量,以便它们没有名称冲突?例如:
foo.c: bar.c:
static int foo = 1; static int foo = 10;
void fooTest() { void barTest() {
static int bar = 2; static int bar = 20;
foo++; foo++;
bar++; bar++;
printf("%d,%d", foo, bar); printf("%d, %d", foo, bar);
} }
Run Code Online (Sandbox Code Playgroud)
如果我编译两个文件并将其链接到重复调用fooTest()和barTest的main,则printf语句将独立增加.有意义,因为foo和bar变量是翻译单元的本地变量.
但是存储分配在哪里?
需要明确的是,假设您有一个工具链可以输出ELF格式的文件.因此,我相信,有有将一些空间,对于那些静态变量的可执行文件保留.
出于讨论目的,我们假设我们使用GCC工具链.
#include <iostream>
using namespace std;
class T1
{
const int t = 100;
public:
T1()
{
cout << "T1 constructor: " << t << endl;
}
};
Run Code Online (Sandbox Code Playgroud)
当我尝试t用100. 初始化const成员变量时,但是它给了我以下错误:
test.cpp:21: error: ISO C++ forbids initialization of member ‘t’
test.cpp:21: error: making ‘t’ static
Run Code Online (Sandbox Code Playgroud)
如何初始化const值?
当我尝试使用以下代码在全局范围中添加const数组时:
static NUMBERS: [i32] = [1, 2, 3, 4, 5];
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
error: mismatched types:
expected `[i32]`,
found `[i32; 5]`
(expected slice,
found array of 5 elements) [E0308]
static NUMBERS2: [i32] = [1, 2, 3, 4, 5];
^~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
我发现处理这个问题的唯一方法是在类型中指定长度:
static NUMBERS: [i32; 5] = [1, 2, 3, 4, 5];
Run Code Online (Sandbox Code Playgroud)
有没有更好的办法?应该可以创建一个数组而无需手动计算其元素.
我想使用Rust和once_cell来实现一些静态const结构实例,并且一个静态const向量包含这些静态结构实例。
这是示例代码:
use once_cell::sync::Lazy;
pub struct Kind {
name: String,
value: i32,
}
impl Kind {
pub fn new(name: &str, value: i32) -> Kind {
Kind {name: String::from(name), value}
}
}
const HYBRID: Lazy<Kind> = Lazy::new(|| Kind::new("a", 1));
const BOND: Lazy<Kind> = Lazy::new(|| Kind::new("b", 2));
// other instances
static KINDS: Lazy<Vec<Kind>> = Lazy::new(|| {
vec![
HYBRID,
BOND,
// other instances,
]
});
Run Code Online (Sandbox Code Playgroud)
这是编译器错误:
use once_cell::sync::Lazy;
pub struct Kind {
name: String,
value: i32,
}
impl Kind {
pub …Run Code Online (Sandbox Code Playgroud) 在Rust中,我有以下代码:
pub trait Test: Sized {
const CONST: Self;
fn static_ref() -> &'static Self {
&Self::CONST
}
}
Run Code Online (Sandbox Code Playgroud)
我的期望是,既然const是'static,那么我应该能够引用也是'static。但是,编译器给出以下错误:
pub trait Test: Sized {
const CONST: Self;
fn static_ref() -> &'static Self {
&Self::CONST
}
}
Run Code Online (Sandbox Code Playgroud)
这里如何引入临时变量?
此外,似乎在某些情况下引用常量确实有效。这是一个简短的具体示例,其中Test的实现略有不同
pub trait Test: Sized {
fn static_ref() -> &'static Self;
}
struct X;
impl Test for X {
fn static_ref() -> &'static Self {
&X
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序,它的字符串处理功能可能有点太多了。我将大部分文字消息移至常量;我不确定这在 Rust 中是否正确,但我习惯用 C 编写。
我发现我不能轻易地使用我的static &str内部match表达式。我可以使用文本本身,但不知道如何正确地做到这一点。
我知道这是一个编译器问题,但不知道如何以 Rust 风格正确编写该结构。我应该使用枚举而不是类似 C 的静态变量吗?
static SECTION_TEST: &str = "test result:";
static STATUS_TEST_OK: &str = "PASSED";
fn match_out(out: &String) -> bool {
let s = &out[out.find(SECTION_TEST).unwrap() + SECTION_TEST.len()..];
match s {
STATUS_TEST_OK => {
println!("Yes");
true
}
_ => {
println!("No");
false
}
}
}
Run Code Online (Sandbox Code Playgroud)
static SECTION_TEST: &str = "test result:";
static STATUS_TEST_OK: &str = "PASSED";
fn match_out(out: &String) -> bool {
let s = &out[out.find(SECTION_TEST).unwrap() + SECTION_TEST.len()..];
match …Run Code Online (Sandbox Code Playgroud) 在include_bytes!与include_str!宏看起来像一个谜给我。我知道该文件包含在二进制文件中,但它在运行时如何工作?
include_bytes!/的结果存储include_str!为顶级const?文件会在应用程序运行的整个持续时间内都在内存中吗?