c中的内存占用部分

memory footprint section in c

你能解释一下当你在 c 中收集内存占用时这些部分是什么意思吗? 我可以看到 .text 是源代码,我假设 .const 和 .data 是全局数据和常量(但不太确定),.bss 是什么意思?

| .text    | .const    | .data     | .bss      |

.bss is/are 未初始化的 static 变量。

// foo ends up in bss (because it is not explicitly initialised)
// it's initial value is whatever the default 'zero' value for the type is.
static int foo;
// bar ends up in .data
// (because) it is initialised with the value 42. 
static int bar = 42;
// baz ends up in .const
// (because) it is initialised with a value (22) and the object is const.
// meaning that the value cannot be allowed to change, meaning the object
// can be safely mapped to read-only memory pages (if supported).
static const int baz = 22;
// code goes in .text:
int main() { return 0; }

您可以找到一些答案 。这也涵盖了 运行 时间管理部分 heapstack(这是原始答案)。

简而言之(扩展):

  • .bss 用于声明为 static 且具有全局范围的未初始化变量。这实际上并未存储在文件中,而只是在调用 `main() 之前的 运行 时间保留和清除。
  • .data 包含明确初始化的变量。
  • .const 包含 const 个声明的对象。
  • .text是存放程序代码的地方。注意不是源代码,而是编译后的程序代码!

正常 "ELF" 目标文件中还有大量其他部分,其中包含调试信息等。

有关详细信息,请阅读目标文件格式。使用最广泛的一种是 ELF.

bss(Block Started by Symbol) 存储零值初始化的静态分配变量(包括 globalstatic 变量)。如果静态分配的初始化值不是 0,例如:

int global = 5;

然后 global 将被分配到 data 部分。