On local and global static variables in C++

2020-01-27 03:38发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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


回答4:

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.



回答5:

The main or most serious difference is time of initialization. Local static variables are initialized on first call to function where they are declared. The global ones are initialized at some point in time before the call to main function, if you have few global static variables they are intialized in an unspecified order, which can cause problems; this is called static initialization fiasco.



回答6:

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



回答7:

  1. They are known to all functions in a program whereas global variables are known only in a limited scope.
  2. Global static variables can be initialized before the program starts whereas local static variables can be initialized as execution reaches point.


标签: c++ static