static data memebr initilization

What would be the issue/problem in case static data memebr initilization will be done within an function, the function has been called mutliple times during programm execution? I mean if we will do a static variable intilization a number of time during program run.

I had tried with coding and seems everything is working properly, wanted to know in case any memory/timing related issue it would trigger?
Static members are basically globals, and are initialized exactly once when the program starts up. The only issue that could happen is if you have one static member's initialization depend on the state of another static member. IE:

1
2
3
// in class1.cpp

int class1::astaticvar = 5;
1
2
3
// in class2.cpp

int class2::anotherstatic = class1::astaticvar + 2;


This is known as the "static initialization order fiasco". Since both statics reside in different compilation units (different source files), there is no way to know which will be initialized first. So class2's variable is trying to read class1's variable... but it might not have been initialized yet.


EDIT:
What would be the issue/problem in case static data memebr initilization will be done within an function, the function has been called mutliple times during programm execution?


Errm... well I might have misunderstood your question, because static members are initialized once globally, they are not initialized in functions.

If you have a static variable that is not a class member, that works a little differently:

1
2
3
4
void afunction()
{
  static int foo = 15;  // <- static local var (not a member var)
}


'foo' will be initialized with 15 the very first time afunction is called. Every time afunction is called after that, it will retain its previous value (it will not be assigned to 15 each time).

The only way this could go wrong is if you are multithreading, and two threads call afunction at the same time without some kind of mutex to guard it.


But maybe this is not what you mean? Can you post an example of what you're doing so I can understand more clearly?
Last edited on
> I mean if we will do a static variable intilization a number of time during program run.

Initialization can be done only once.


> wanted to know in case any memory/timing related issue it would trigger?

In general, no (C++11).
If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization .... The implementation must not introduce any deadlock around execution of the initializer.


Unless:
If control re-enters the declaration recursively while the variable is being initialized, the behavior is undeļ¬ned.


If concurrency is not involved, it is well-behaved in C++98 too.

Try this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void foo( int i )
{
    std::cout << "in foo: i == " << i << '\n' ;
    if( i%3 == 2 )
    {
        std::cout << "\tentering block containing static variable\n" ;
        static const void* p = std::cout << "\t*** dynamic initialization of static\n" ;
    }
}

int main ()
{
    for( int i = 0 ; i<10 ; ++i ) foo(i) ;
}



Topic archived. No new replies allowed.