简体   繁体   English

c ++在“本地范围”中初始化静态变量

[英]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).具有静态存储持续时间的变量,其中初始值在编译时已知, 零初始化常量初始化是静态初始化并在其他一切之前初始化(在 C++11 和更早的标准修订版中, 零初始化在常量初始化之前)。 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 .剩下动态初始化,如果我正确阅读 C++ 标准中的[basic.start.static] ,静态初始化都发生在任何翻译单元中的任何动态初始化之前。 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.不, static局部变量有一个明确定义的初始初始化顺序。 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.你可以用一个线程来解决这个问题,但是 C++11 更好地确保一个线程不能中断静态变量的初始化。 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.main之前分配和初始化非局部变量,因此通常在您有机会调用包含静态局部变量的函数之前,它们已被初始化并准备好使用。

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在 A.cpp 中说我们有

int A_variable  = something_dynamic();

and in B.cpp we have在 B.cpp 中,我们有

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 . B_variable初始化可能发生在A_variable初始化之前。 This will call func and set local_static to the as-yet undefined value in B_variable .这将调用func并将local_static设置为B_variable尚未定义的值。 Ooops.哎呀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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