简体   繁体   English

c中的内存占用部分

[英]memory footprint section in c

could you explain what these section mean when you collect memory footprint in c? 当您在c中收集内存占用量时,您能否解释这些部分的含义? I can see .text is source code and I assume .const and .data are global data and constants(but not too sure) and what does .bss mean? 我可以看到.text是源代码,我假设.const和.data是全局数据和常量(但不太确定),. bss是什么意思?

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

Some answer you can find here . 您可以在这里找到一些答案。 That covers also the run-time managed sections heap and stack (that was the original answer). 这也涵盖了运行时托管节的 (这是原始答案)。

In short (an extended): 简而言之(扩展):

  • .bss is for uninitialized variables declared static and with global scope. .bss用于声明为static且具有全局范围的未初始化变量。 This is not actually stored in the file, but just reserved and cleared at run-time right before `main() is called. 它实际上并没有存储在文件中,只是在调用`main()之前在运行时保留并清除了。
  • .data contains explicitly initialized variables. .data包含显式初始化的变量。
  • .const contains const declared objects. .const包含const声明的对象。
  • .text is where the program code is stored. .text是程序代码的存储位置。 Note that is not the source code, but the compiled program code! 注意这不是源代码,而是编译后的程序代码!

There is also a plethora of other sections in normal "ELF" object files which contain debugging information, etc. 普通的“ ELF”对象文件中还有许多其他部分,其中包含调试信息等。

For more information, read about object file formats. 有关更多信息,请阅读有关目标文件格式。 One of the most widely used is ELF . 使用最广泛的一种是ELF

.bss is/are uninitialised static variables. .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; }

bss ( Block Started by Symbol ) stores the zero-value initialised static-allocated variables (including global and static variables). bssBlock Started by Symbol bss Block Started by Symbol )存储零值初始化的静态分配变量(包括global变量和static变量)。 If the initialised value of a static-allocated is not 0 , eg: 如果静态分配的初始化值不为0 ,例如:

int global = 5;

Then the global will be allocated in data section. 然后,将在data部分中分配global变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM