On local and global static variables in C++

前端 未结 7 1069
野的像风
野的像风 2020-11-29 05:55

C++ Primer says

Each local static variable is initialized before the first time execution passes through the object\'s definition. Local statics are

相关标签:
7条回答
  • 2020-11-29 06:14

    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
    
    0 讨论(0)
  • 2020-11-29 06:17
    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.
    0 讨论(0)
  • 2020-11-29 06:21

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

    0 讨论(0)
  • 2020-11-29 06:25

    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.

    0 讨论(0)
  • 2020-11-29 06:26

    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

    0 讨论(0)
  • 2020-11-29 06:31

    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.

    0 讨论(0)
提交回复
热议问题