Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Static variable declared in different scopes

Tags:

c++

static

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;
    }
like image 814
Jigyasa Avatar asked Aug 09 '13 05:08

Jigyasa


2 Answers

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.

For those who care about the specific time at which a block-level static variable is initialized, 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
like image 91
Jerry Coffin Avatar answered Nov 15 '22 02:11

Jerry Coffin


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.
like image 26
Mahesh Avatar answered Nov 15 '22 04:11

Mahesh