简体   繁体   中英

c++ initialization static variable in "local scope"

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM