关于 C++ 中的局部和全局静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12186857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
On local and global static variables in C++
提问by James Raitsev
C++ Primer says
C++ 入门 说
Each local static variable is initialized before the first time execution passes through the object's definition. Local statics are not destroyed when a function ends; they are destroyed when program terminates.
每个局部静态变量在第一次执行通过对象的定义之前被初始化。函数结束时不会破坏局部静态;它们在程序终止时被销毁。
Are local static variables any different from global static variables? Other then the location where they are declared, what else is different?
局部静态变量与全局静态变量有什么不同吗?除了它们被声明的位置之外,还有什么不同?
void foo () {
static int x = 0;
++x;
cout << x << endl;
}
int main (int argc, char const *argv[]) {
foo(); // 1
foo(); // 2
foo(); // 3
return 0;
}
compare with
与之比较
static int x = 0;
void foo () {
++x;
cout << x << endl;
}
int main (int argc, char const *argv[]) {
foo(); // 1
foo(); // 2
foo(); // 3
return 0;
}
回答by Mike Seymour
The differences are:
区别在于:
- The name is only accessible within the function, and has no linkage.
- It is initialised the first time execution reaches the definition, not necessarily during the program's initialisation phases.
- 该名称只能在函数内访问,并且没有链接。
- 它在第一次执行到达定义时被初始化,不一定在程序的初始化阶段。
The second difference can be useful to avoid the static intialisation order fiasco, where global variables can be accessed before they're initialised. By replacing the global variable with a function that returns a reference to a local static variable, you can guarantee that it's initialised before anything accesses it. (However, there's still no guarantee that it won't be destroyed before anything finishes accessing it; you still need to take great care if you think you need a globally-accessible variable. See the comments for a linkto help in that situation.)
第二个区别对于避免静态初始化顺序 fiasco很有用,其中可以在初始化之前访问全局变量。通过将全局变量替换为返回对局部静态变量的引用的函数,您可以保证它在任何访问它之前已经初始化。(但是,仍然不能保证在任何内容访问它之前它不会被销毁;如果您认为需要一个全局可访问的变量,您仍然需要非常小心。请参阅评论以获取在这种情况下提供帮助的链接。 )
回答by Ricket
Their scope is different. A global-scoped static variable is accessible to any function in the file, while the function-scoped variable is accessible only within that function.
它们的范围不同。文件中的任何函数都可以访问全局范围的静态变量,而函数范围的变量只能在该函数内访问。
回答by emon
Hopefully, this example will help to understand the difference between static local and global variable.
希望这个例子有助于理解静态局部变量和全局变量之间的区别。
#include <iostream>
using namespace std;
static int z = 0;
void method1() {
static int x = 0;
cout << "X : " << ++x << ", Z : " << ++z << endl;
}
void method2() {
int y = 0;
cout << "Y : " << ++y << ", Z : " << ++z << endl;
}
int main() {
method1();
method1();
method1();
method1();
method2();
method2();
method2();
method2();
return 0;
}
output:
输出:
X : 1, Z : 1
X : 2, Z : 2
X : 3, Z : 3
X : 4, Z : 4
Y : 1, Z : 5
Y : 1, Z : 6
Y : 1, Z : 7
Y : 1, Z : 8
回答by Martin York
There real name is:
真正的名字是:
static storage duration object.
Global variables are also 'static storage duration object'. The major difference from global variables are:
全局变量也是“静态存储持续时间对象”。与全局变量的主要区别是:
- They are not initialized until the first use
Note: An exception during construction means they were not initialized and thus not used.
So it will re-try the next time the function is entered. - Their visability is limited by their scope
(ie they can not be seen outside the function)
- 它们直到第一次使用时才会被初始化
注意:构造期间的异常意味着它们没有被初始化,因此没有被使用。
所以下次进入函数时会重试。 - 它们的
可见性受其作用域的限制(即它们在函数之外无法被看到)
Apart from that they are just like other 'static storage duration objects'.
除此之外,它们就像其他“静态存储持续时间对象”一样。
Note: Like all 'static storage duration objects' they are destroyed in reverse order of creation.
注意:像所有“静态存储持续时间对象”一样,它们以与创建相反的顺序销毁。
回答by piokuc
The main or most serious difference is time of initialization. Local static variables are initialized on first call to function where they are declared. The global ones are initialized at some point in time before the call to main function, if you have few global static variables they are intialized in an unspecified order, which can cause problems; this is called static initialization fiasco.
主要或最严重的区别是初始化时间。局部静态变量在第一次调用它们被声明的函数时被初始化。全局变量在调用 main 函数之前的某个时间点进行初始化,如果全局静态变量很少,它们会以未指定的顺序初始化,这可能会导致问题;这称为静态初始化失败。
回答by georges
In your first code block, x is local to the foo() function which means that it is created in foo() and destroyed at the end of the function after cout. However, in your second block x is global which means that the scope of x is the entire program. If you wanted to under int main your could cout << x << endl and it would print however, in the first block it would say x not declared
在您的第一个代码块中,x 是 foo() 函数的局部变量,这意味着它是在 foo() 中创建的,并在 cout 之后的函数末尾销毁。但是,在您的第二个块中 x 是全局的,这意味着 x 的范围是整个程序。如果您想在 int main 下,您可以 cout << x << endl 并且它会打印但是,在第一个块中它会说 x 未声明
回答by eric njora murigi
- They are known to all functions in a program whereas global variables are known only in a limited scope.
- Global static variables can be initialized before the program starts whereas local static variables can be initialized as execution reaches point.
- 程序中的所有函数都知道它们,而全局变量仅在有限的范围内知道。
- 全局静态变量可以在程序启动之前初始化,而局部静态变量可以在执行到达点时初始化。