Difference between Static variable declared in dif

2020-02-12 11:56发布

问题:

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

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

    int main()
    {
       static int c;
       while(condition)
       {
           static int d;
            ....
       }
    some code here;
    return 0;
    }

回答1:

Ultimately, there is no difference. Ignoring (for the moment) static member functions, static means what it means -- but we see different parts of what it means under different conditions because some of what it means can also happen without the keyword.

When you use the static keyword, the object being defined always has:

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

Both of these are true about a static variable whether defined inside or outside a block. One part or the other will happen by default, even if you don't use the static keyword, but if you use the keyword, you always get both.

static member functions are analogous, but since they're functions they don't exactly have lifetime -- all functions have static lifetime. A static member function has local visibility (i.e., its name is visible only with its class) and something sort of like "static lifetime" -- the function isn't bound to an instance of the class.

Edit: for those who care about the specific time at which a block-level static variable is intialized, the gory details are as follows (§6.7/4):

The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered.

An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization.

So, the variable will be zero-initialized very early in the startup of the program. Then, if other initialization has been specified, that will happen no later than when execution passes through the initialization (but could happen earlier than that). Note, however, the difference between constant initialization and other initialization. Just 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 constant initialized, but y is not. Since x is constant initialized, it is initialized upon entry to the block, so when we print out its value, we should get 1. y, however, is not constant initialized, and the goto means that execution never flows through its initialization -- therefore, it will retain the 0 that it was initialized with before any other initialization took place, so (with a properly functioning compiler) the output will be:

1 
0


回答2:

Static variable inside a block(local static variable) -

  1. It is not visible outside the block/function
  2. It's value retains in function calls as it is static

Static variable outside a block(Global static variable) -

  1. It's scope is the entire file(like a in your program)
  2. It's value retains between function calls as it is static.


回答3:

static int a;

means the variable a is a file scope variable, i.e, it can't be seen from other files.

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

means the local variable b has a life cycle that goes from the program starts to the program ends, i.e, you can assign it some value, while on the next call of the function, it remembers that value.

c and d are similar to b.



回答4:

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;
}


回答5:

static int a;//file scope variable
void getVol()
{
    static int b;//fixed duration
}

File scoped variables act exactly like global variables, except their use is restricted to the file in which they are declared (which means you can not extern them to other files). A fixed duration variable is one that retains it’s value even after the scope in which it has been created has been exited! Fixed duration variables are only created (and initialized) once, and then they are persisted throughout the life of the program. Check this link:http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/



回答6:

Using the static keyword in block scope is exactly like a global variable with respect to where it is stored, however the difference is that it is only accessible within the block scope that it was declared. Recall that as you nest blocks the inner-most block takes precedence when referencing an identifier -- which the same applies to static variables.

Consider the following snippet of code to illustrate my response:

#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;
}


标签: c++ static