About local and global static variables in C ++

C ++ Primer says

Each local static variable is initialized until the first time execution passes through the definition of the object. Local statics are not destroyed when a function ends; they are destroyed when the program terminates.

Are local static variables different from global static variables? Another place where they are announced, what is still wrong?

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; } 
+13
source share
7 answers

The differences are as follows:

  • The name is only available inside the function and has no binding.
  • Initialized the first time that execution reaches a definition, not necessarily during program initialization phases.

The second difference may be useful in order to avoid the fiasco of the static initialization order, where global variables can be accessed before they are initialized. By replacing a global variable with a function that returns a reference to a local static variable, you can guarantee its initialization before anything gets it. (However, there is still no guarantee that it will not be destroyed before anything ends its access, you still need to be very careful if you think you need a globally accessible variable. See Comments for, to help in this situation.)

+18
source

Their difference is different. A static variable with global scope is available for any function in the file, and a variable with a functional area is available only inside this function.

+5
source

Real name:

 static storage duration object. 

Global variables are also "static storage duration objects." The main difference from global variables:

  • They do not initialize until first use.
    Note. An exception during construction means that they were not initialized and therefore were not used.
    Thus, he will try again the next time the function is entered.
  • There visibility is limited by their area.
    (i.e. they are not visible outside the function)

In addition, they are similar to other "static storage duration objects".

Note. Like all "static storage duration objects", they are destroyed in the reverse order of creation.

+4
source

Hope this example helps to understand the difference between a 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; } 

exit:

 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 
+3
source

The main or most serious difference is the initialization time. Local static variables are initialized the first time the function is called, where they are declared. Global ones are initialized at some point in time before calling the main function, if you have several global static variables that they are initialized in an unspecified order, which can cause problems; this is called a static initialization fiasco.

+1
source

In your first block of code, x is local to the function foo (), which means that it is created in foo () and destroyed at the end of the function after cout. However, in the second block, x is global, which means that the scope of x is the entire program. If you want under int main you could cout <x <endl and it will print, however, in the first block it would say that x is not declared

0
source
  • They are known to all functions of the program, while global variables are known only to a limited extent.
  • Global static variables can be initialized before the program starts, while local static variables can be initialized since execution reaches a point.
0
source

All Articles