106

I'm quite confident that globally declared variables get allocated (and initialized, if applicable) at program start time.

int globalgarbage;
unsigned int anumber = 42;

But what about static ones defined within a function?

void doSomething()
{
  static bool globalish = true;
  // ...
}

When is the space for globalish allocated? I'm guessing when the program starts. But does it get initialized then too? Or is it initialized when doSomething() is first called?

Lundin
  • 195,001
  • 40
  • 254
  • 396
Owen
  • 7,494
  • 10
  • 42
  • 52

8 Answers8

105

I was curious about this so I wrote the following test program and compiled it with g++ version 4.1.2.

include <iostream>
#include <string>

using namespace std;

class test
{
public:
        test(const char *name)
                : _name(name)
        {
                cout << _name << " created" << endl;
        }

        ~test()
        {
                cout << _name << " destroyed" << endl;
        }

        string _name;
};

test t("global variable");

void f()
{
        static test t("static variable");

        test t2("Local variable");

        cout << "Function executed" << endl;
}


int main()
{
        test t("local to main");

        cout << "Program start" << endl;

        f();

        cout << "Program end" << endl;
        return 0;
}

The results were not what I expected. The constructor for the static object was not called until the first time the function was called. Here is the output:

global variable created
local to main created
Program start
static variable created
Local variable created
Function executed
Local variable destroyed
Program end
local to main destroyed
static variable destroyed
global variable destroyed
Adam Pierce
  • 33,531
  • 22
  • 69
  • 89
  • 39
    As a clarification: the static variable is initialized the first time execution hits its declaration, not when the containing function is called. If you just have a static at the start of the function (e.g. in your example) these are the same, but are not necessarily: e.g. if you have 'if (...) { static MyClass x; ... }', then 'x' will not be initialized at ALL during the first execution of that function in the case where the if statement's condition evaluates to false. – EvanED Feb 12 '14 at 20:42
  • 5
    But doesn't this lead to a runtime overhead, since each time the static variable is used, the program has to check if it has been previously used, since if not, it has to be initialized? In that case that kind of sucks a bit. – HelloGoodbye Aug 14 '14 at 08:15
  • perfect illustration – Des1gnWizard Dec 31 '15 at 01:18
  • 1
    @veio: Yes, the initialization is thread-safe. See that question for more details: https://stackoverflow.com/questions/23829389/does-a-function-local-static-variable-automatically-incur-a-branch – Rémi Oct 23 '18 at 12:41
  • 2
    @HelloGoodbye: yes, it leads to a runtime overhead. Also see that question: https://stackoverflow.com/questions/23829389/does-a-function-local-static-variable-automatically-incur-a-branch – Rémi Oct 23 '18 at 12:42
  • Why would you not expect that the static variable inside the function would only be initialized when the function is used? My problem is cpp initializing the variable before the function is called and causing UB. Anyhow, questions like these require the cpp standards, not just a test. – user13947194 Oct 21 '21 at 22:10
  • @HelloGoodbye overhead compared to what? You pay for what you use, not more – 463035818_is_not_an_ai Dec 06 '21 at 13:02
  • 1
    @463035818_is_not_a_number compared to if the static variable had been initialized at the beginning of the program – HelloGoodbye Dec 06 '21 at 15:26
  • @HelloGoodbye If you want a variable to be initialised at program start up then make is a global variable. If you are worried about access to that variable then put it and the functions that need to access it in their own translation unit. The point is that delaying the initialisation of static locals is useful, not every variable can be initialised at program start up. – john Jun 05 '23 at 09:56
  • @john Creating a global variable for something that shouldn't be global feels like it goes directly against C++'s philosophy. – HelloGoodbye Jun 06 '23 at 11:58
66

Some relevant verbiage from C++ Standard:

3.6.2 Initialization of non-local objects [basic.start.init]

1

The storage for objects with static storage duration (basic.stc.static) shall be zero-initialized (dcl.init) before any other initialization takes place. Objects of POD types (basic.types) with static storage duration initialized with constant expressions (expr.const) shall be initialized before any dynamic initialization takes place. Objects of namespace scope with static storage duration defined in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit. [Note: dcl.init.aggr describes the order in which aggregate members are initialized. The initialization of local static objects is described in stmt.dcl. ]

[more text below adding more liberties for compiler writers]

6.7 Declaration statement [stmt.dcl]

...

4

The zero-initialization (dcl.init) of all local objects with static storage duration (basic.stc.static) is performed before any other initialization takes place. A local object of POD type (basic.types) with static storage duration initialized with constant-expressions is initialized before its block is first entered. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope (basic.start.init). Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control re-enters the declaration (recursively) while the object is being initialized, the behavior is undefined. [Example:

      int foo(int i)
      {
          static int s = foo(2*i);  // recursive call - undefined
          return i+1;
      }

--end example]

5

The destructor for a local object with static storage duration will be executed if and only if the variable was constructed. [Note: basic.start.term describes the order in which local objects with static storage duration are destroyed. ]

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
  • 3
    This answered my question and doesn't rely on "anecdotal evidence" unlike the accepted answer. I was specifically looking for this mention of exceptions in the constructor of statically-initialized function local static objects: `If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration.` – Bensge Mar 11 '20 at 15:07
32

The memory for all static variables is allocated at program load. But local static variables are created and initialized the first time they are used, not at program start up. There's some good reading about that, and statics in general, here. In general I think some of these issues depend on the implementation, especially if you want to know where in memory this stuff will be located.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Eugene
  • 755
  • 1
  • 8
  • 13
  • 2
    not quite, local statics are allocated and zero-initialized "at program load" (in quotes, because that's not quite right either), and then reinitialized the first time the function they're in is entered. – Mooing Duck Apr 12 '12 at 21:02
  • Looks like that link is now broken, 7 years later. – Steve Oct 14 '15 at 19:26
  • 2
    Yep, link broke. Here's an archive: https://web.archive.org/web/20100328062506/http://www.acm.org/crossroads/xrds2-4/ovp.html – Eugene Nov 06 '15 at 22:57
10

The compiler will allocate static variable(s) defined in a function foo at program load, however the compiler will also add some additional instructions (machine code) to your function foo so that the first time it is invoked this additional code will initialize the static variable (e.g. invoking the constructor, if applicable).

@Adam: This behind the scenes injection of code by the compiler is the reason for the result you saw.

Henk
  • 1,704
  • 10
  • 12
8

I try to test again code from Adam Pierce and added two more cases: static variable in class and POD type. My compiler is g++ 4.8.1, in Windows OS(MinGW-32). Result is static variable in class is treated same with global variable. Its constructor will be called before enter main function.

  • Conclusion (for g++, Windows environment):
  1. Global variable and static member in class: constructor is called before enter main function (1).
  2. Local static variable: constructor is only called when execution reaches its declaration at first time.
  3. If Local static variable is POD type, then it is also initialized before enter main function (1). Example for POD type: static int number = 10;

(1): The correct state should be: "before any function from the same translation unit is called". However, for simple, as in example below, then it is main function.

#include <iostream>                     
#include <string>

using namespace std;

class test
{
public:
   test(const char *name)
            : _name(name)
    {
            cout << _name << " created" << endl;
    }

    ~test()
    {
            cout << _name << " destroyed" << endl;
    }

    string _name;
    static test t; // static member
 };
test test::t("static in class");

test t("global variable");

void f()
{
    static  test t("static variable");
    static int num = 10 ; // POD type, init before enter main function
    
    test t2("Local variable");
    cout << "Function executed" << endl;
}

int main()
{
    test t("local to main");
    cout << "Program start" << endl;
    f();
    cout << "Program end" << endl;
    return 0;
 }

result:

static in class created
global variable created
local to main created
Program start
static variable created
Local variable created
Function executed
Local variable destroyed
Program end
local to main destroyed
static variable destroyed
global variable destroyed
static in class destroyed

Anybody tested in Linux env ?

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Thang Le
  • 1,419
  • 1
  • 17
  • 25
  • 2
    how did you conclude that `num` is initialized before `main`? Note that if the initializer is something more complicated than an integer literal this is not the case: https://godbolt.org/z/Gasr7dTY8 obvioulsy in this example, expected side-effects would be off it was initialized before `main` – 463035818_is_not_an_ai Dec 06 '21 at 12:57
4

Or is it initialized when doSomething() is first called?

Yes, it is. This, among other things, lets you initialize globally-accessed data structures when it is appropriate, for example inside try/catch blocks. E.g. instead of

int foo = init(); // bad if init() throws something

int main() {
  try {
    ...
  }
  catch(...){
    ...
  }
}

you can write

int& foo() {
  static int myfoo = init();
  return myfoo;
}

and use it inside the try/catch block. On the first call, the variable will be initialized. Then, on the first and next calls, its value will be returned (by reference).

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
dmityugov
  • 4,390
  • 23
  • 18
3

Static variables are allocated inside a code segment -- they are part of the executable image, and so are mapped in already initialized.

Static variables within function scope are treated the same, the scoping is purely a language level construct.

For this reason you are guaranteed that a static variable will be initialized to 0 (unless you specify something else) rather than an undefined value.

There are some other facets to initialization you can take advantage off -- for example shared segments allow different instances of your executable running at once to access the same static variables.

In C++ (globally scoped) static objects have their constructors called as part of the program start up, under the control of the C runtime library. Under Visual C++ at least the order that objects are initialized in can be controlled by the init_seg pragma.

Rob Walker
  • 46,588
  • 15
  • 99
  • 136
  • 6
    This question is about function-scoped statics. At least when they have nontrivial constructors they are initialized on first entry into the function. Or more specifically, when that line is reached. – Adam Mitz Sep 11 '08 at 00:22
  • True -- but the question talks about the space allocated to the variable, and uses simple data types. The space is still allocated in the code segment – Rob Walker Sep 11 '08 at 00:44
  • I don't see how code segment vs. data segment really matters here. I think we need clarification from the OP. He did say "and initialized, if applicable". – Adam Mitz Sep 11 '08 at 00:48
  • 5
    variables are never allocated inside the code segment; this way they wouldn't be write-able. – botismarius Sep 12 '08 at 13:11
  • 1
    static variables are allocated space in data segment or in bss segment depending upon whether they are initialized or not. – EmptyData Nov 15 '17 at 06:50
-2

In the following code it prints Initial = 4 which is the value of static_x as it is implemented in the compiling time.

 int func(int x)
    {
        static int static_x = 4;
        static_x = x;
        printf ("Address = 0x%x",&static_x );   // prints 0x40a010
        return static_x;
    }
    int main()
    {
        int x = 8;
        uint32_t *ptr = (uint32_t *)(0x40a010); // static_x location
        printf ("Initial = %d\n",*ptr);
        func(x);
    
        return 0;
    }
Sherif Beshr
  • 23
  • 1
  • 8