2

Static variables are initialized at the beginning of a program, and as described in here, the initialization order of static variable is undefined. As a result, initializing one static variable with another one might cause the program to crash.

Is it also true to 'local' static variable declared in function?

Is it safe to use global declared static variable to initialize 'local' static variable?

And if I have several static variables inside the same local scope, is it safe to use early declared one to initialize late declared one?

Ziqi Liu
  • 2,931
  • 5
  • 31
  • 64
  • 1
    Local statics with dynamic initialization are initialized when execution reaches that line – M.M Feb 07 '20 at 23:54
  • All static storage duration variables that are not local variables defined with a single translation unit are created and destroyed in the order they are defined in the translation unit. The problem described at the link only occurs if you have multiple translation units and static storage duration variables depending on static storage duration variables in another translation unit because you cannot control which translation unit will be initialized first. This is not a problem with static variables in a function. The function cannot split up across multiple translation units, for one thing. – user4581301 Feb 08 '20 at 00:02
  • @user4581301 your first sentence only applies to dynamic initialization. Static initialization happens first, e.g. at file scope `int a = foo(); int b = 5; int c = bar();` , the `b` is guaranteed to have value `5` during execution of `foo` – M.M Feb 08 '20 at 02:07
  • Thanks, @M.M I suspected something like that (if the compiler knows what's going into that variable, why not burn it in at compile time?) but I couldn't find confirmation. Now that I've spent a bit more time [and found it](https://en.cppreference.com/w/cpp/language/initialization#Non-local_variables) I've got a bit of fixing in the answer I pitched. – user4581301 Feb 08 '20 at 05:38

1 Answers1

2

A variable with static storage duration where the initial value is known at compile time, zero initialized or constant initialized is Static Initialized and initialized before everything else (With zero initialization preceding constant initialization in C++11 and older Standard revisions). After all, if the compiler and linker know exactly what value is going into that variable at compile time, why wouldn't they just put the value there?

Statically initialized variables are not a problem. If you're assigning a constant or a zero, the variable doesn't depend on anything else. That leaves dynamic initialization, and If I'm reading [basic.start.static] in the C++ Standard correctly, static initialization all happens before any dynamic initialization in any translation unit. The problem is with dynamically initialized variables with static storage duration interacting across multiple translation units. You can guarantee the order of initialization within a translation unit, in order of definition, but you cannot guarantee the order in which translation units are initialized.

Is it also true to 'local' static variable declared in function?

No. static local variables have a well defined initial initialization order. Dynamic initialization will be occur on first use and they can't be split across translation units, and that eliminates the ambiguity in non-local initialization ordering.

And if I have several static variables inside the same local scope, is it safe to use early declared one to initialize late declared one?

Yes. Again we have a well defined initialization order. Dynamically initialized variables will be initialized in order and statically initialized variables are already initialized. You can mess this up with a thread, but C++11 and better ensure that one thread cannot interrupt the initialization of a static variable. If a thread interrupts between the initialization of two static variables, it's on you whether that's safe or not, but the first variable will still be initialized before the second.

Is it safe to use global declared static variable to initialize 'local' static variable?

Not always. A non-local variable is allocated and initialized before main, so normally they are initialized and ready for use before you get a chance to call the function containing the static local variable.

But what about initializing with a function that contains a static variable that depends on a variable from a different translation unit?

Say in A.cpp we have

int A_variable  = something_dynamic();

and in B.cpp we have

int  func()
{
    static int local_static = A_variable;
    return local_static;
}
int B_variable = func();

Initialization of B_variable may happen before initialization of A_variable. This will call func and set local_static to the as-yet undefined value in B_variable. Ooops.

user4581301
  • 33,082
  • 7
  • 33
  • 54