C ++中POD类型的默认初始化 [英] Default initialization of POD types in C++

查看:138
本文介绍了C ++中POD类型的默认初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一些 POD 变量在默认情况下初始化,但其他不。 (POD类型包括 int float ,指针,联合,POD类型数组,POD类型的结构等。 )

I know some POD variables are initialized by default, but others are not. (POD types include int, float, pointers, unions, arrays of POD types, structs of POD types, etc.)

范围和存储类如何影响POD类型的默认初始化?


  • 自动存储的局部变量

  • 静态全局变量

  • 外部变量

  • 分配给

  • 一个类的POD成员(在构造函数中没有显式初始化)

  • Local variables with automatic storage
  • Static local variables
  • Static global variables
  • External variables
  • Variables allocated with new
  • POD members of a class (without explicit initialization in a constructor)

我知道有一些与这些情况有关的问题,但没有全面的问题(它们只针对特定情况)。

I know there are existing questions relating to some of these situations, but none comprehensive (they only address specific situations).

推荐答案

没有自动初始化具有自动存储持续时间的局部变量。由于使用未初始化的变量会产生未定义的行为,因此,即使在变量多余的情况下,也可以显式初始化变量。

Local variables with automatic storage duration are not being initialized automatically. Since using uninitialized variables produces undefined behavior, it is a good practice to explicitly initialize your variables even when it's redundant.

C ++ 03标准 3.6.2初始化非本地对象状态:

About POD types that are being zero-initialized, C++03 standard 3.6.2 Initialization of non-local objects states:


§1具有静态存储持续时间(3.7.1)的对象必须在进行任何其他初始化之前初始化(8.5)。具有常数表达式的零初始化和初始化统称为静态初始化;所有其他初始化是动态初始化。

§1 Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (3.9) with static storage duration initialized with constant expressions (5.19) shall be initialized before any dynamic initialization takes place.

因此,通过标准保证具有静态存储持续时间(无论其范围是什么)的POD类型将被初始化为零。

So it's guaranteed by standard that POD types with static storage duration (whatever their scope is) will be zero-initialized.


类的POD成员(在构造函数中没有显式初始化)

POD members of a class (without explicit initialization in a constructor)

(选择的部分)的 12.6.2初始化基础和成员中所述:

This situation is described in 12.6.2 Initializing bases and members, that states (selected parts):


em>非静态数据成员或基类不是由mem-initializer-id命名的(包括没有mem-initializer-list的情况,因为构造函数没有ctor-initializer),那么:

If a given nonstatic data member or base class is not named by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer), then:

- 如果实体是非静态数据成员 ...,实体类是非POD 类,实体默认初始化(8.5)...

— If the entity is a nonstatic data member..., and the entity class is a non-POD class, the entity is default-initialized (8.5)...

- 否则,实体未初始化。 ..

在调用类X的构造函数完成后,如果X的成员既没有在构造函数的mem初始化器中指定,也没有被初始化,也没有值初始化,在执行构造函数主体期间也不给定值,成员具有不确定的值。

After the call to a constructor for class X has completed, if a member of X is neither specified in the constructor’s mem-initializers, nor default-initialized, nor value-initialized, nor given a value during execution of the body of the constructor, the member has indeterminate value.

示例:

class C
{
public:
    C(int x, int z) : x(x), z(z) { }
    int x, y, z;
};

int main(void)
{
    C* c = new C(1,3);
    std::cout << c->y; // value of y is undetermined !!!
}

这篇关于C ++中POD类型的默认初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆