默认情况下如何初始化局部变量和全局变量?

2022-01-18 00:00:00 initialization c++

根据下面,我说的对吗?

Based on below, am i right?

  • global_A 引用被初始化为 null.
  • global_int 为 0
  • local_A 引用为空
  • local_int 未初始化
  • global_A.x 和 local_A.x 均未初始化.

感谢您的帮助.

A global_A;
int global_int;

class A {
  public : int x;
}

int main()
{
  int local_int;
  A local_A;
}

推荐答案

建立在 Andrey 的响应之上.

Building up on Andrey's response.

$3.6.2-在任何其他初始化发生之前,具有静态存储持续时间 (3.7.1) 的对象应进行零初始化 (8.5).".在 OP 中,global_A"和global_int"具有静态存储持续时间.local_int"和local_A"没有链接,因为它们是本地对象.

$3.6.2- "Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.". In OP, "global_A" and "global_int" have static storage duration. "local_int" and "local_A" have no linkage as these are local objects.

$8.5/5- 对 T 类型的对象进行零初始化意味着:

$8.5/5- To zero-initialize an object of type T means:

――如果 T 是标量类型 (3.9),则对象设置为值 0(零)转换为 T;

― if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;

――如果 T 是非联合类类型,则每个非静态数据成员和每个基类子对象是零初始化;

― if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;

――如果 T 是联合类型,则对象的第一个命名的数据成员 89) 是零初始化;

― if T is a union type, the object’s first named data member89) is zero-initialized;

――如果 T 是一个数组类型,每个元素是零初始化的;

― if T is an array type, each element is zero-initialized;

――如果 T 是引用类型,则否执行初始化.

― if T is a reference type, no initialization is performed.

$6.7.4/4-在任何其他初始化发生之前执行所有具有静态存储持续时间 (3.7.1) 的本地对象的零初始化 (8.5).具有静态的 POD 类型 (3.9) 的本地对象用常量表达式初始化的存储持续时间在第一次进入它的块之前被初始化.允许执行一个实现在允许实现在命名空间范围(3.6.2)中静态初始化具有静态存储持续时间的对象的相同条件下,早期初始化具有静态存储持续时间的其他本地对象.否则,此类对象在控件第一次通过其声明时被初始化;这样的对象在其初始化完成时被认为已初始化.如果初始化抛出异常退出,说明初始化未完成,下次控件进入声明时会再次尝试.如果控件在初始化对象时(递归地)重新进入声明,则行为未定义."

$6.7.4/4- "The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any other initialization takes place. A local object of POD type (3.9) with static storage duration initialized with constant-expressions is initialized before its block is first entered. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope(3.6.2). Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control re-enters the declaration (recursively) while the object is being initialized, the behavior is undefined."

编辑 2:

$8.5/9- "如果没有初始化器为一个对象指定,并且对象属于(可能是 cv 限定的)非 POD 类类型(或其数组),对象应为默认初始化;如果对象是const 限定类型,基础类类型应具有用户声明的默认构造函数.否则,如果没有为非静态对象指定初始化器,则对象及其子对象(如果有),有一个不确定的首字母value90);如果对象或任何它的子对象是 const 限定的类型,程序格式错误."

$8.5/9- "If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value90); if the object or any of its subobjects are of const-qualified type, the program is ill-formed."

一般来说,您需要阅读这些部分以及 8.5 美元,以便更好地掌握这方面的信息.

In general, you want to read up these sections along with $8.5 for good hold on this aspect.

相关文章