The difference between a static variable declared in different areas

What is the difference between declaring a static variable inside a block and outside a block in a file? For example, what is the difference between static variables a, b, c, d? Can we declare a static variable accessible from all program files?

static int a; void getVol(..) { static int b; } int main() { static int c; while(condition) { static int d; .... } some code here; return 0; } 
+4
source share
6 answers

Ultimately, there is no difference. Ignoring (for now) static member functions, static means what it means, but we see different parts of what it means in different conditions, because part of what it means can also happen without a keyword.

When you use the static , the designated object always has:

  • static lifetime - it exists for the entire life of the program.
  • local visibility - the name is not displayed outside the scope in which it is declared.

Both of these values โ€‹โ€‹refer to a static variable, regardless of whether they are defined inside or outside the block. One part or the other will happen by default, even if you do not use the static , but if you use the keyword, you always get both.

static member functions are similar, but since they are functions, they do not have a lifetime, all functions have a static lifetime. A static member function has local visibility (that is, its name is visible only with its class) and something like a โ€œstatic lifetimeโ€ - the function is not tied to an instance of the class.

Edit: for those interested in a specific time, when the static variable of the block level is inalistic, the gory details are as follows (ยง6.7 / 4):

Zero initialization (8.5) of all block volume variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization. The constant initialization (3.6.2) of a block region object with a static storage duration, if applicable, is performed before its block is first entered.

Implementations are allowed to perform early initialization of other variables of the block area with static or storage streams of threads under the same conditions that implementations are allowed to statically initialize a variable with statics or storage duration of streams in the namespace space (3.6.2). Otherwise, such a variable is initialized when the first control passes through its declaration; such a variable is considered initialized after completion of its initialization.

Thus, the variable will be initialized to zeros very early when the program starts. Then, if another initialization is specified, this will happen no later than when the execution passes through the initialization (but may happen earlier than this). Note, however, the difference between constant initialization and other initialization. For example, consider something like this:

 int g() { return 2; } int f() { goto bypass; static int x = 1; static int y = g(); bypass: std::cout << x << "\n" << y; } 

Here x is an initialized constant, but y not. Since x is a constant initialization, it is initialized when entering a block, so when we print its value, we should get 1 . y , however, is not constant initialization, and goto means that execution never proceeds through its initialization, therefore it will save 0 , which was initialized before any other initialization occurred, therefore (with the correct working compiler), exit will be:

 1 0 
+10
source
 static int a; 

means that the variable a is a variable of the file region, that is, it cannot be seen from other files.

 void getVol(..) { static int b; } 

means that the local variable b has a life cycle that goes from the program, starts at the end of the program, i.e. you can assign it some value, and the next time you call the function, it remembers that value.

c and d are similar to b .

+1
source

Static variable inside the block ( local static variable ) -

  • It is not visible outside the block / function
  • This value is preserved when calling functions, because it is static

Static variable outside the block ( Global static variable ) -

  • This area is the whole file (for example, a in your program)
  • This value is stored between function calls, because it is static.
+1
source

The following worked for me:

 /* * How to create an object on the stack. * Also make sure that only 5 objects are created for the class */ #include <iostream> using namespace std; class User { private: int id; static int counter; static bool isOk; public: User(); ~User() {} int getId() { return id; } static int getCounter() { return counter; } static bool getStatus() { return isOk; } static void resetOk() { isOk = false; } static void setOk() { isOk = true; } }; User::User() { if(counter == 5) { cout << "Not allowed to create more than 5 objects" << endl; resetOk(); return; } counter++; id = counter; setOk(); } int User::counter = 0; bool User::isOk = false; int main() { // Create objects on stack User user1; (User::getStatus()) ? cout << "user1 id: " << user1.getId() << endl : cout << "Object Construction Failed" << endl; User user2; (User::getStatus()) ? cout << "user2 id: " << user2.getId() << endl : cout << "Object Construction Failed" << endl; User user3; (User::getStatus()) ? cout << "user3 id: " << user3.getId() << endl : cout << "Object Construction Failed" << endl; User user4; (User::getStatus()) ? cout << "user4 id: " << user4.getId() << endl : cout << "Object Construction Failed" << endl; User user5; (User::getStatus()) ? cout << "user5 id: " << user5.getId() << endl : cout << "Object Construction Failed" << endl; User user6; (User::getStatus()) ? cout << "user6 id: " << user6.getId() << endl : cout << "Object Construction Failed" << endl; User user7; (User::getStatus()) ? cout << "user7 id: " << user7.getId() << endl : cout << "Object Construction Failed" << endl; return 0; } 
+1
source

Using a static keyword in a block area is exactly like a global variable as to where it is stored, but the difference is that it is only available within the block area that it declared. Recall that when blocking blocks, the largest block takes precedence when referencing an identifier, which is the same with static variables.

Consider the following code snippet to illustrate my answer:

 #include <stdio.h> void f1( void ) { static int a = 10; { static int a = 9; printf( "inner a = %d\n", a ); } printf( "outer a = %d\n", a ); } int main( void ) { for ( int i = 0; i < 10; i++ ) { printf( "Calling the function f1.\n" ); f1(); } return 0; } 
0
source
 static int a;//file scope variable void getVol() { static int b;//fixed duration } 

File-bound variables act exactly like global variables, except that their use is limited to the file in which they are declared (which means that you cannot derive them from other files). A constant time variable is one that retains its value even after the area in which it was created was discovered! Fixed duration variables are only created (and initialized) once, and then stored throughout the life of the program. Check out this link: http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/

0
source

Source: https://habr.com/ru/post/1496087/


All Articles