简体   繁体   English

存储静态变量的位置(数据段或堆或BSS)?

[英]Where are static variables stored (data segment or heap or BSS)?

I obtained conflicting Opinions about static variable storage. 我获得了关于静态变量存储的矛盾意见。

Opinion 1 : "A stack static variable stores its value in the heap " 意见1: 堆栈静态变量将其值存储在堆中

Opinion 2 : "A stack static variable stores its value in the data segment". 意见2: 堆栈静态变量将其值存储在数据段中”。

I am confused with these conflicting answers. 我对这些相互矛盾的答案感到困惑。

Where exactly are static variables stored? 哪里存储了静态变量?

I am expecting an answers with references (text books, authentic tutorials, etc.). 我期待着参考文献的答案(教科书,真实的教程等)。

Static variables have two types: 静态变量有两种类型:

  1. static variables declared inside a function. 在函数内声明的静态变量。
  2. global (declared outside function) static variable. 全局(声明的外部函数)静态变量。

I would also like to know if there is any difference in the storage of these two types of variables? 我还想知道这两种变量的存储是否存在差异?

The 'stack variables' are usually stored on 'the stack', which is separate from the text, data, bss and heap sections of your program. '堆栈变量'通常存储在'堆栈'中,它与程序的文本,数据,bss和堆部分分开。

The second half of your question is about 'static' variables, which are different from stack variables - indeed, static variables do not live on the stack at all. 你的问题的后半部分是关于'静态'变量,它们与堆栈变量不同 - 实际上,静态变量根本不存在于堆栈中。 Classically, static variables would all be in the data or bss sections of your program. 传统上,静态变量都将出现在程序的数据或bss部分中。 With modern compilers, if the data is const-qualified, then the data may be stored in the text section of your program, which has a variety of benefits (including enforced non-modifiability). 对于现代编译器,如果数据是const限定的,那么数据可以存储在程序的文本部分中,这具有多种好处(包括强制的非可修改性)。

The C standard does not dictate that there is a stack, nor a bss section. C标准没有规定存在堆栈,也没有bss部分。 It just requires storage space to be available for variables with appropriate durations. 它只需要存储空间可用于具有适当持续时间的变量。

Stack memory is allocated when you launch your application and always remains the same size during the execution of the application. 启动应用程序时会分配堆栈内存,并且在执行应用程序期间始终保持相同的大小。 It's not stored in the DATA segment, DATA segment is for things like constant values used in the application (such as string literals). 它不存储在DATA段中,DATA段用于应用程序中使用的常量值(例如字符串文字)。

Both local and global static variables are kept in initialized Data segments 本地和全局静态变量都保存在初始化的数据段中

There are two data segments initialized data segment and unitialized data segment. 初始化数据段和单元化数据段有两个数据段。

Unitialized data segment also called BSS. 单位数据段也称为BSS。

When we say data segment, by default it initialized data segment, this section gets copied from loaded image of the program. 当我们说数据段时,默认情况下它初始化数据段,这部分从程序的加载图像中复制。 ( all global variables and local static variables initialized to to non zero ie ini var1_global = 10; ) (所有全局变量和局部静态变量初始化为非零,即ini var1_global = 10;)

The uninitialized data segemnet aka BSS. 未初始化的数据segemnet又称BSS。 This section will be initialized to zero generall, just before main() gets called. 在调用main()之前,此部分将初始化为零generall。 All unitialized global, local static goes here. 所有单元化的全局本地静态都在这里。

http://www.geeksforgeeks.org/memory-layout-of-c-program/ http://www.geeksforgeeks.org/memory-layout-of-c-program/

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

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