简体   繁体   English

C++静态POD初始化

[英]c++ static POD initialization

I have a single instance of a simple POD我有一个简单的 POD 实例

a.hpp一个.hpp

class A {
    struct Zzz {
        A*  m_aPtr;
        int m_val;
    }

    static Zzz s_zzz;
};

a.cpp a.cpp

A::Zzz A::s_zzz;

I expect that both s_zzz.m_aPtr and s_zzz.m_val will be initialized to zeros before any other static initialization in any other compilation unit and it is guaranteed by the language itself.我希望 s_zzz.m_aPtr 和 s_zzz.m_val 都将在任何其他编译单元中的任何其他静态初始化之前初始化为零,并且由语言本身保证。 Am I right about it?我是对的吗?

Usually I provide default constructors for the structs.通常我为结构提供默认构造函数。 Say

A::Zzz::Zzz() :
 m_aPtr(0),
 m_val(0)
{
}

Will it create initialization order problem or introduce compiler dependencies?它会产生初始化顺序问题还是引入编译器依赖性?

At least in C++0x, you can rely on all zero-initialization being performed before any other initialization code runs.至少在 C++0x 中,您可以依赖在任何其他初始化代码运行之前执行的所有零初始化

From the C++0x FCD, section [basic.start.init]来自 C++0x FCD,部分[basic.start.init]

Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place.具有静态存储持续时间 (3.7.1) 或线程存储持续时间 (3.7.2) 的变量应在任何其他初始化发生之前进行零初始化 (8.5)。

If you're considering using this variable from other initialization code, then an explicit constructor would be a big mistake, as it would run sometime mixed in with other initialization code, and overwrite whatever changes have already been made by other initializers.如果您正在考虑从其他初始化代码使用此变量,那么显式构造函数将是一个大错误,因为它有时会与其他初始化代码混合运行,并覆盖其他初始化程序已经进行的任何更改。

I expect that both s_zzz.m_aPtr and s_zzz.m_val will be initialized to zeros before any other static initialization in any other compilation unit and it is guaranteed by the language itself.我希望 s_zzz.m_aPtr 和 s_zzz.m_val 都将在任何其他编译单元中的任何其他静态初始化之前初始化为零,并且由语言本身保证。

It will be zero-initialized, since it's a static lifetime variable at namespace scope.它将被零初始化,因为它是命名空间范围内的静态生命周期变量。

That zero-initialization happens before any dynamic initialization (an example of dynamic initializatin is when you some explicit initialization, or the class has a constructor).零初始化发生在任何动态初始化之前(动态初始化的一个例子是当您进行一些显式初始化或类具有构造函数时)。

The order of zero-initialization between different translation units is not defined, but there's not any way to detect it or rely on it since it happens before anything else, so it doesn't matter.不同翻译单元之间的零初始化顺序没有定义,但没有任何方法可以检测或依赖它,因为它发生在其他任何事情之前,所以这无关紧要。

Re your point 2, it's rather unclear what you're asking about.关于你的第 2 点,目前还不清楚你在问什么。

But for your static lifetime object, the effect is just that it's first zero-initialized, and then during dynamic initialization your constructor is used to zero it again (although the compiler might be smart enough to optimize away that redundant extra initialization).但是对于你的静态生命周期对象,效果只是它首先被初始化为零,然后在动态初始化期间你的构造函数被用来再次将它归零(尽管编译器可能足够聪明,可以优化掉多余的额外初始化)。

Cheers & hth.,干杯 & hth.,

ERRATA : Ben Voigt has provided a convincing example that the last paragraph above is wrong .勘误表:Ben Voigt 提供了一个令人信服的例子,说明上面最后一段是错误的 So please disregard.所以请无视。 The presence of the constructor means that the object can be dynamically initialized at some point before, between or after operations that change it, causing rather unpredictable results…构造函数的存在意味着对象可以在改变它的操作之前、之间或之后的某个时刻动态初始化,从而导致相当不可预测的结果......

  1. There are no guarantees about initialization order of statics between compilation units (see http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 ).编译单元之间的静态初始化顺序无法保证(参见http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14 )。

  2. If it has a constructor, it will no longer be a POD, unfortunately.不幸的是,如果它有一个构造函数,它将不再是一个 POD。

  1. Static data is always initialised to zero.静态数据始终初始化为零。
  2. No it shouldn't introduce any initialisation problems.不,它不应该引入任何初始化问题。

When the application is loaded into memory, the static area is initialised to zero.当应用程序加载到内存中时,静态区域被初始化为零。 This is before any code starts to execute.这是在任何代码开始执行之前。

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

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