数组初始化 [英] array initialisation

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

问题描述

我相当肯定,内建类型的数组未初始化,而的UDT数组是默认情况下初始化。

I'm quite certain that arrays of built in types are unitialized, whereas arrays of UDTs are default initialized.

INT富[5]; //将含有垃圾结果
富富[5]; //将包含在默认情况下初始化5美孚对象

这不管阵列是否栈或堆分配上出现。

This occurs regardless of whether the array is allocated on the stack or heap.

不过,我发现很难找到这样的一个权威来源。比亚说:

However, I'm finding it hard to find an authoritative source on this. Bjarne states that:

阵列和结构的成员是默认初始化或不依赖于数组或结构是静态的,它并没有真正告诉我太多了。

"Members of arrays and structures are default initialized or not depending on whether the array or structure is static" which doesn't really tell me too much.

我也试图找到在标准的东西,但至今没有无济于事。

I've also tried to find something in the standard, but so far no to no avail.

有谁知道权威人士的证实上述的?

Does anyone know of an authoritative source to confirm the above?

推荐答案

ISO C ++ 03大约是权威的,因为它得到:

ISO C++03 is about as authoritative as it gets:

一个POD结构是具有类型的非POD结构,非POD联合(或这些类型的数组)或引用没有非静态数据成员的集合类,并没有用户定义的复制赋值运营商也没有用户定义的析构函数。同样,POD工会是具有类型的非POD结构,非POD联合(或这些类型的数组)或引用没有非静态数据成员的合盟,并且有没有用户定义的复制赋值运算符没有用户定义的析构函数。豆荚类是一类,要么是POD结构或POD联合。

A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor. A POD class is a class that is either a POD-struct or a POD-union.

运算类型(3.9.1),枚举类型,指针类型,以及指向成员类型(3.9.2),和这些类型(3.9.3)的CV-合格版本统称标量类型。标量类型,POD结构类型,POD联合类型(第9条),这种类型以及这些类型的CV-合格版本(3.9.3)的阵列统称POD类型。

Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.

要零初始化类型T的对象是指:

To zero-initialize an object of type T means:


      
  • 如果T是一个标量类型(3.9),该对象被设置为0(零)的值变换至T

  •   
  • 如果T是一个非工会类型,每个非静态数据成员并且每个基础类
      子对象初始化为零;

  •   
  • 如果T是一个联合类型,对象的第一个指定数据成员初始化为零;

  •   
  • 如果T是数组类型,每个元素都是零初始化;

  •   
  • 如果T是引用类型,不执行初始化。

  •   

要默认初始化类型T的对象是指:

To default-initialize an object of type T means:


      
  • 如果T是一个非POD类类型(第9条),T的默认构造函数(并形成不良的初始化,如果T没有访问的默认构造函数);

  •   
  • 如果T是数组类型,每个元素都是默认初始化;

  •   
  • ,否则,该对象被初始化为零。

  •   

要值初始化类型T的对象是指:

To value-initialize an object of type T means:


      
  • 如果T是一个类类型(第9条)与用户声明的构造(12.1),则T的默认构造函数(并形成不良的初始化,如果T没有访问的默认构造函数);

  •   
  • 如果T是无用户声明的构造一个非工会类类型,那么T的每一个非静态数据成员和基类成分是值初始化;

  •   
  • 如果T是一个数组类型,那么每一个元素都是值初始化;

  •   
  • ,否则,该对象被零初始化

  •   

静态存储持续时间的每个对象应是任何其他初始化发生之前在程序启动时初始化为零。 [注意:在某些情况下,额外的初始化以后完成]

Every object of static storage duration shall be zero-initialized at program startup before any other initialization takes place. [Note: in some cases, additional initialization is done later.]

其初始是空的括号,即()一个对象,应是值初始化。

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

如果一个对象没有指定初始化,并且该对象是(可能CV修饰)非POD类型(或阵列及其),对象应是缺省初始化的;如果对象是const限定的类型,底层类类型应有一个用户声明的默认构造函数。否则,如果用于非静态对象没有指定初始化,对象及其子对象,如果有的话,有一个不确定的初始值);如果对象或其子对象是const限定型的,是非法的构造方案。

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 value); if the object or any of its subobjects are of const-qualified type, the program is ill-formed.

有关你的榜样, INT 绝对是一个POD类型(这是一个算术型),因此,本地或类型的字段 INT ,在没有初始化的,将有一个不确定的值。对于,这取决于它是如何定义的 - 粗略地说,如果没有一个构造函数,它的所有成员都是POD类型,那么它本身就是一个POD类型,没有初始化发生无论是。否则,默认构造函数被调用。即便如此,这并不意味着的成员的初始化 - 规则是递归的,所以非POD类型的POD成员不会被初始化,除非该类型的构造函数明确这是否(在其初始化列表)。

For your example, int is definitely a POD type (it's an arithmetic type), and therefore a local or field of type int, in the absence of initializer, will have an indeterminate value. For Foo, this depends on how it is defined - roughly speaking, if it doesn't have a constructor, and all its members are of POD types, then it is itself a POD type, and no initialization takes place either. Otherwise, the default constructor is called. Even then, this doesn't mean that members are initialized - rules are recursive, so POD members of non-POD type won't be initialized unless the constructor of that type specifically does that (in its initializer list).

静态变量和字段将在所有情况下进行零初始化。请注意,这适用于非吊舱太 - 这意味着一个类类型的静态变量保证有各个领域的递归设置为(T)0 甚至在它的构造函数运行

Static variables and fields will in all cases be zero-initialized. Note that this applies to non-PODs too - meaning that a static variable of a class type is guaranteed to have all fields recursively set to (T)0 even before its constructor runs.

一个方便的技巧为默认初始化任何集合POD类型是使用 {} 在初始化 - 请注意,它的工作原理与结构以及数组:

A handy trick to default-initialize any aggregate POD type is to use {} in initializer - note that it works with structs as well as arrays:

char s[10] = {}; // all elements default-initialized
Foo foo = {};    // all fields recursively default-initialized

这篇关于数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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