简体   繁体   English

静态存储在内存中的全局变量在哪里?

[英]Where are global variables which are static stored in memory?

In C,the global variables are stored in data segment.But I have no clue where will static variables which are global get stored? 在C语言中,全局变量存储在数据段中。但是我不知道全局的静态变量将存储在哪里?

static int num;   //where will this variable get stored in memory?
int main(){
   //code 

}    

There are various places these variables may be stored, the standard does not mandate any particular storage location. 这些变量可以在各种地方存储,标准没有规定任何特定的存储位置。 Depending on situation they may even be stored in read-only memory (if not modified) or not at all (if not used). 根据情况,它们甚至可以存储在只读存储器中(如果未修改)或根本不存储(如果不使用)。 It is even not required that the location need to be fixed (or single). 甚至不需要固定位置(或单个位置)。

The only requirement the standard puts regarding storage is that the variable has to keep it's value across function calls. 标准对存储的唯一要求是变量必须在函数调用之间保持其值。

However a normal approach would be to place these together with the global variables. 但是,通常的方法是将它们与全局变量放在一起。 The only thing that differs static variables from global are the scope of the symbols. 静态变量和全局变量唯一不同的是符号的范围。 However note that this is only one possibility and I can imagine other reasonable solutions. 但是请注意,这只是一种可能性,我可以想象其他合理的解决方案。

To get a more precise answer you need to know what platform and toolchain you are using. 要获得更精确的答案,您需要知道您使用的平台和工具链。

它们存储在内存的数据段中。

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

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