本文介绍了静态变量在哪里存储(数据段或堆或BSS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到关于冲突的静态变量存储意见。

I obtained conflicting Opinions about static variable storage.

意见1:静态变量存储它的值在

Opinion 1 : static variable stores its value in the heap"

意见2:静态变量存储其在数据值的段

Opinion 2 : static variable stores its value in the data segment".

我很困惑与这些相互矛盾的答案。

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.).

静态变量有两种类型:


  1. 函数内声明的静态变量。

  2. 全球(外函数声明)静态变量。

我也想知道是否有这两种类型的变量的存储有什么区别?

I would also like to know if there is any difference in the storage of these two types of variables?

推荐答案

在'堆栈变量的通常存储在栈,这是从你的程序的文本,数据,BSS和堆部分分开。

The 'stack variables' are usually stored on 'the stack', which is separate from the text, data, bss and heap sections of your program.

你的问题的第二部分是关于静态变量,这是从堆栈变量不同 - 事实上,静态变量不住在栈上的。经典,静态变量都将是在你的程序的数据或BSS部分。随着现代编译器,如果数据是const限定,则该数据可能存储在你的程序,其中有各种各样的好处(包括强迫非可修改)的文本部分。

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. 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).

C标准没有规定,有一个栈,也不是bss段。它只是需要存储空间,可用于适当的持续时间变量。

The C standard does not dictate that there is a stack, nor a bss section. It just requires storage space to be available for variables with appropriate durations.

这篇关于静态变量在哪里存储(数据段或堆或BSS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 20:08