C++ Primer says
C ++ Primer说
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;
}
7 个解决方案
#1
24
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 link to help in that situation.)
第二个差异可以用于避免静态初始化顺序惨败,其中全局变量可以在初始化之前被访问。通过用返回对本地静态变量的引用的函数替换全局变量,可以保证在访问它之前初始化它。 (但是,仍然无法保证在完成访问之前它不会被销毁;如果您认为需要一个全局可访问的变量,您仍需要非常小心。请参阅注释以获取有关该情况的帮助链接。 )
#2
6
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.
它们的范围不同。全局范围的静态变量可供文件中的任何函数访问,而函数范围的变量只能在该函数中访问。
#3
5
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
#4
4
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. - There 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.
注意:与所有“静态存储持续时间对象”一样,它们以与创建相反的顺序被销毁。
#5
2
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函数之前的某个时间点初始化全局函数,如果你有很少的全局静态变量,它们会以未指定的顺序初始化,这可能会导致问题;这称为静态初始化惨败。
#6
0
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没有声明
#7
0
- 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.
它们对程序中的所有函数都是已知的,而全局变量仅在有限的范围内是已知的。
全局静态变量可以在程序启动之前初始化,而本地静态变量可以在执行到达点时初始化。
#1
24
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 link to help in that situation.)
第二个差异可以用于避免静态初始化顺序惨败,其中全局变量可以在初始化之前被访问。通过用返回对本地静态变量的引用的函数替换全局变量,可以保证在访问它之前初始化它。 (但是,仍然无法保证在完成访问之前它不会被销毁;如果您认为需要一个全局可访问的变量,您仍需要非常小心。请参阅注释以获取有关该情况的帮助链接。 )
#2
6
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.
它们的范围不同。全局范围的静态变量可供文件中的任何函数访问,而函数范围的变量只能在该函数中访问。
#3
5
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
#4
4
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. - There 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.
注意:与所有“静态存储持续时间对象”一样,它们以与创建相反的顺序被销毁。
#5
2
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函数之前的某个时间点初始化全局函数,如果你有很少的全局静态变量,它们会以未指定的顺序初始化,这可能会导致问题;这称为静态初始化惨败。
#6
0
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没有声明
#7
0
- 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.
它们对程序中的所有函数都是已知的,而全局变量仅在有限的范围内是已知的。
全局静态变量可以在程序启动之前初始化,而本地静态变量可以在执行到达点时初始化。