以下rust代码,在x86_64平台编译。
#[macro_use]
extern crate lazy_static;
use std::collections::HashMap;
lazy_static! {
static ref HASHMAP: HashMap<u32, &'static str> = {
let mut m = HashMap::new();
m.insert(0, "foo");
m.insert(1, "bar");
m.insert(2, "baz");
m
};
}
fn main() {
// First access to `HASHMAP` initializes it
println!("The entry for `0` is \"{}\".", HASHMAP.get(&0).unwrap());
// Any further access to `HASHMAP` just returns the computed value
println!("The entry for `1` is \"{}\".", HASHMAP.get(&1).unwrap());
}
我使用readelf命令查看符号表中HASHMAP变量的大小:
readelf -sW target/debug/deps/section_test-4d7d6a03c56fdde3.o
Symbol table '.symtab' contains 590 entries:
Num: Value Size Type Bind Vis Ndx Name
452: 0000000000000000 0 OBJECT LOCAL DEFAULT 498 _ZN12section_test7HASHMAP17hbc6de818c577d166E
我们可以看到Size是0。
readelf -SW target/debug/deps/section_test-4d7d6a03c56fdde3.o
There are 527 section headers, starting at offset 0x98690:
Section Headers:
[Nr] Name Type Address Off Size ES Flg Lk Inf Al
[498] .rodata._ZN12section_test7HASHMAP17hbc6de818c577d166E PROGBITS 0000000000000000 00b528 000000 00 A 0 0 1
我们看一下符号所在的部分,符号位于rodata部分并且该部分的Size也为0。
既然长度为0,是不是意味着不能存储数据了,运行时HashMap内存分配到哪里了? C语言符号表中可以定义长度为0的变量吗?