区别“全球”和“静态全局” [英] Difference between 'global' and 'static global'

查看:117
本文介绍了区别“全球”和“静态全局”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A 全局变量的范围中的所有文件,而 静态的全局变量的范围仅仅是文件的地方声明。为什么这样?

A global variable's scope is in all the files, while a static global variable's scope is just the file where it is declared. Why so?

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

Where are global or static global variables stored in memory?

推荐答案

有一些混乱,因为静态在C可意味着两个不同的东西。一个是静态存储持续时间,另一种是内部连接。 静态用作文件范围的关键字会给功能,或者使用具有内部链接的对象。

There is some confusion, since static in C can mean two different things. One is static storage duration, and the other is internal linkage. static used as a keyword on file-scope will give the function or object it is used with internal linkage.

有关的函数或对象内部连接意味着,如果声明中另一个文件的另一个功能(这是不是真的被称为文件,而是翻译单元 - TU),那么该声明将提到的一不同的的功能:在该单元中声明的名称将链接到一个不同的实体比其他翻译单元中声明的名称,这是内部到恩。这同样适用于对象。

Internal linkage for a function or object means that if you declare another function in another "file" (this is not really called "file", but rather translation unit - TU), then that declaration will refer to a different function: The name declared in that unit will "link" to a different entity than the name declared in that other translation unit, which was "internal" to that TU. The same applies to objects.

不管是不是文件范围变量声明为静态,它仍然具有静态存储时间:这意味着它生活在整个程序,并且死的时候该程序将终止。具有静态存储持续时间的对象的另一个例子是文字串。凡具有静态存储时间存储对象没有规定,但它们通常存储取决于他们是否被初始化与否:初始化文件范围的变量通常存储在一个名为。数据部分,而没有初始化文件-Scope变量通常存储在一个名为.bss段一节。请注意,如果该变量没有被初始化,这将是零的节目的开始初始化:本的.bss部分通常是零由程序的启动的实现初始化。

Whether or not a file-scope variable is declared with static, it will still have a static storage duration: That means it lives through the whole program, and dies when the program terminates. Another example of an object that has static storage duration is a string literal. Where objects that have static storage duration are stored isn't specified, but they are usually stored depending on whether they are initialized or not: Initialized file-scope variables are usually stored in a section called ".data", while non-initialized file-scope variables are usually stored in a section called ".bss". Remember that if the variable isn't initialized, it will be zero initialized at the start of the program: The ".bss" section is usually zero initialized by an implementation on program's startup.

我说:通常是无处不在,因为那里的东西都存储没有规定。例如,一些实施方式可以存储在只读部分字符串。如果你有一个文件范围的指针,不初始化它,实现它初始化为空指针,​​这并不一定适用于所有空字节的对象:)

I said "usually" everywhere, since where things are stored isn't specified. For example, some implementations could store string literals in a read-only section. And if you have a file-scope pointer and don't initialize it, the implementation initializes it to a null-pointer, which is not necessarily an object with all null bytes :)

这篇关于区别“全球”和“静态全局”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆