On local and global static variables in C++

C++ Primer says

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

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.)


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.


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

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.
  • Their 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.