【发布时间】:2020-12-19 03:26:00
【问题描述】:
如何在 cpp 中声明一个全局 const 对象,以便其关联的数据完全存储在 .rodata 中,而不是在运行时初始化期间创建并不必要地复制该对象?
例如,如果我创建类型的全局变量
const std::stringconst std::array<const std::string, 4>const std::map<std::string, const std::string>
测试告诉我这些将编译成 .bss,因此需要运行时初始化,尽管它是编译时已知的常量数据......并且它需要知道将它们初始化为什么,因此它也不必要地复制数据,使用额外的内存。
如何在没有任何运行时初始化的情况下获取驻留在 .rodata 中的实际 const 对象?
由于 C++ 标准可能对此不够具体,如果您需要一些编译器特定的功能,g++ 和/或 clang++ 支持的功能将不胜感激。
注意:请,如果您的答案是某种提升或某种特定库的东西,请解释该库是如何实现这一点的。我想了解这是如何实现的。
以下注释可以忽略,但包括在内,因为我不断从人们那里得到的最初反应是“不可能,一个 const 字符串或 const 数组不需要运行时初始化或重复数据”。
所以这里有一个例子和一些测试:
test.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <array>
#include <map>
std::string str {"here"};
const std::string cstr {"there"};
std::array<std::string, 3> arr {"eight", "six", "seven"};
const std::array<const std::string, 4> carrc {"five", "nine", "oh", "three"};
const std::map<std::string, const std::string> cmapc = {
{"a", "apple"},
{"b", "bananna"},
{"c", "carrot"},
};
void show_info(const char *name, const void *a, const void *b)
{
std::cout << name << "\t" << a << " " << b << std::endl;
}
int main(int argc, char **argv) {
#define INFO(x) show_info(#x, &x, x.data())
INFO(str);
INFO(cstr);
INFO(arr);
INFO(arr[1]);
INFO(carrc);
INFO(carrc[1]);
std::cout << "cmapc" << "\t" << (void *)&cmapc << std::endl;
INFO(cmapc.at("a"));
std::ifstream infile("/proc/self/maps");
std::string line;
while(std::getline(infile, line)) {
std::cout << line << std::endl;
}
return 0;
}
编译并检查对象的放置位置
$ g++ -std=c++17 -o test test.cpp
$ readelf -W -S test | grep -E "(.rodata|.data|.bss)"
[16] .rodata PROGBITS 0000000000005ad0 005ad0 0000e9 00 A 0 0 8
[24] .data PROGBITS 0000000000209000 009000 000018 00 WA 0 0 8
[25] .bss NOBITS 0000000000209020 009018 000290 00 WA 0 0 32
$ readelf -s test | grep OBJ | grep -E "[^_](str|arr|map)"
37: 00000000002091e0 32 OBJECT LOCAL DEFAULT 25 _ZL4cstr
38: 0000000000209200 128 OBJECT LOCAL DEFAULT 25 _ZL5carrc
39: 0000000000209280 48 OBJECT LOCAL DEFAULT 25 _ZL5cmapc
88: 0000000000209160 96 OBJECT GLOBAL DEFAULT 25 _Z3arrB5cxx11
105: 0000000000209140 32 OBJECT GLOBAL DEFAULT 25 _Z3strB5cxx11
您也可以只运行程序并查看输出。
或者查看 gdb 中的运行时初始化
$ gdb -q ./test
Reading symbols from ./test...(no debugging symbols found)...done.
(gdb) b _start
Breakpoint 1 at 0x2180
(gdb) r
Starting program: /tmp/test
Breakpoint 1, 0x0000000008002180 in _start ()
(gdb) x/4gx &str
0x8209140 <_Z3strB5cxx11>: 0x0000000000000000 0x0000000000000000
0x8209150 <_Z3strB5cxx11+16>: 0x0000000000000000 0x0000000000000000
(gdb) b main
Breakpoint 2 at 0x800230f
(gdb) c
Continuing.
Breakpoint 2, 0x000000000800230f in main ()
(gdb) x/4gx &str
0x8209140 <_Z3strB5cxx11>: 0x0000000008209150 0x0000000000000004
0x8209150 <_Z3strB5cxx11+16>: 0x0000000065726568 0x0000000000000000
【问题讨论】:
-
std::string为堆上的实际字符分配内存。我不相信有办法避免对其进行动态初始化。 -
与
std::map相同,它如何在编译时创建它的树? -
您根据哪个标准编译代码?采用
const char *的std::string的构造函数是在C++20 中创建的constexpr。我尚未对其进行测试,但这表明短字符串可能会在编译时构建。 -
@IgorTandetnik 看看我的例子,对于全局 const std::string 它不会在堆上。它在 .bss
-
@Someprogrammerdude 它知道编译时的所有数据,所以至少看起来可行。
标签: c++ memory-management constants