On local and global static variables in C++

2020-01-27 03:50发布

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

标签: c++ static
7条回答
三岁会撩人
2楼-- · 2020-01-27 04:02

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.

查看更多
等我变得足够好
3楼-- · 2020-01-27 04:05
  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.
查看更多
淡お忘
4楼-- · 2020-01-27 04:06

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

查看更多
混吃等死
5楼-- · 2020-01-27 04:06

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
查看更多
小情绪 Triste *
6楼-- · 2020-01-27 04:07

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楼-- · 2020-01-27 04:13

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.

查看更多
登录 后发表回答