C++ 中结构的存储持续时间和成员初始化

Storage Duration and Member Initialization of a Struct in C++

#include <iostream>

using namespace std;

struct A {
    // Some Other Code
    int x;
};

A a1;

int main(){
    A a2;
    cout << "a1.x = " << a1.x << endl;
    cout << "a2.x = " << a2.x << endl;
    return 0;
}

C++14 标准(ISO/IEC 14882:2014)第 8.5 节第 12 段:

If no initializer is specified for an object, the object is default-initialized. When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (5.17). [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. - end note ]

那么 a1 有静态存储期限吗? a2 有自动存储期限吗? Struct A 的定义在全局命名空间,以及 a1 声明,而 a2 声明在块范围(在 main() 函数内)。

此外,第 3.6.2 节说:

第 1 段:

Non-local variables with static storage duration are initialized as a consequence of program initiation.

第 2 段:

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.

此外,对于 gcc 5.4.0,我得到一个警告 (warning: 'a2.A::x' is used uninitialized in this function [-Wuninitialized]) 和一个随机值,但是对于 clang 3.8.0,输出总是 a2.x = 0(零也可以是不确定的价值)。我还做了其他更复杂的实验,嵌套 structs 和默认初始化放在 // Some Other Code 中。有时我得到随机值,而其他时候(不可忽略的数量)我得到零。

有趣的链接:

C++ Structure Initialization

Initializing default values in a struct

Are members of a C++ struct initialized to 0 by default?

Default initialization of POD types in C++

Initialization of Objects with Static Storage Duration in C vs C++

C and C++ : Partial initialization of automatic structure

How to initialize structures to all-elements-zero-or-null

What is the Storage Duration of Struct A?

这个问题没有意义,存储取决于A如何使用。


And that of objects a1 and a2?

a1 有静态存储。 See this question for more details.

a2有自动存储。


And that of variables a1.x and a2.x?

它们的存储方式与其父 A 实例相同。


Do the same rules apply to class and union?

是的。

So does a1 have Static Storage Duration and does a2 have Automatic Storage Duration?

The definition of Struct A is at global namespace ...

这无关紧要。

... is at global namespace, as well as a1 declaration

a1 具有静态存储持续时间并且是零初始化的。

while a2 declaration is at block scope

a2 是非静态块局部变量。它具有自动存储持续时间并且默认初始化。它有一个不确定的值。


What is the Storage Duration of Struct A?

类型没有存储期限。对象做。

... And that of objects a1 and a2?

见上文。

.. And that of variables a1.x and a2.x?

子对象(包括非静态数据成员)继承其超对象的生命周期。

... Shoud a1.x and a2.x be zero-initialized? Or are they indeterminate values?

a1 为零初始化。这包括它的子对象。 a2 不是。这包括它的子对象。

Do the same rules apply to class and union?

在存储 classes 时,同一组规则适用于联合和非联合 classes 以及非 class 类型的对象。不同类型的默认初始化含义不同。

Does it make any difference the code in // Some Other Code? For example, if a constructor of class T "has non-static members with default initializers" then the default constructor can not be trivial (i.e. perform no action).

编译器默认生成的非平凡默认构造函数初始化没有默认初始化器的成员。