【发布时间】:2015-06-06 01:57:46
【问题描述】:
当您在 c 中收集内存占用时,您能解释一下这些部分的含义吗? 我可以看到 .text 是源代码,我假设 .const 和 .data 是全局数据和常量(但不太确定),.bss 是什么意思?
| .text | .const | .data | .bss |
【问题讨论】:
当您在 c 中收集内存占用时,您能解释一下这些部分的含义吗? 我可以看到 .text 是源代码,我假设 .const 和 .data 是全局数据和常量(但不太确定),.bss 是什么意思?
| .text | .const | .data | .bss |
【问题讨论】:
.bss 是未初始化的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; }
【讨论】:
static 变量。它适用于所有零初始化变量,static 与否。
bss(Block Started by Symbol) 存储零值初始化的静态分配变量(包括global 和static 变量)。如果静态分配的初始化值不是0,例如:
int global = 5;
那么global会被分配到data部分。
【讨论】: