在c++中默认初始化为什么原始类型?

时间:2022-02-16 16:27:17

When I use an initialization list:

当我使用初始化列表时:

struct Struct {
    Struct() : memberVariable() {}
    int memberVariable;
};

the primitive type (int, bool, float, enum, pointer) member variable is default-initialied. Is the value it gets implementation defined or is it the same for all implementations?

原始类型(int、bool、float、enum、指针)成员变量是默认初始化的。是定义了实现的值,还是所有实现的值都相同?

6 个解决方案

#1


30  

You are not correct. The object is not default-initialized but value-initialized. And its value is well-defined

你是不正确的。对象不是默认初始化的,而是值初始化的。它的价值是明确的。

int = 0, 
bool = false, 
float = 0.0f,  
enum = (enum type)0, 
pointer = null pointer
pointer to member = null member pointer

Note that zero is in the range of values for any enumeration, even if it doesn't contain an explicit enumerator with that vaue, so it's safe to initialize an enumeration variable to that value.

注意,0在任何枚举的值范围内,即使它不包含带有vaue的显式枚举数,因此将枚举变量初始化为该值是安全的。

In particular for pointer to data members, the representation used in practice is not all-zero bits. In the so-called C++ Itanium ABI used by at least GCC and Clang, pointer to data members have an all-one bits null representation.

特别是对于指向数据成员的指针,在实践中使用的表示不是全为零的位。在至少由GCC和Clang使用的所谓的c++ Itanium中,指向数据成员的指针具有全- 1位空表示。

#2


27  

The Standard says (8.5/5)

标准的说(8.5/5)

To default-initialize an object of type T means:

默认初始化类型为T的对象意味着:

— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if Thas no accessible default constructor);

-如果T是一种非pod类类型(第9条),则调用T的默认构造函数(如果没有可访问的默认构造函数,则初始化形式不佳);

— if T is an array type, each element is default-initialized;

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

— otherwise, the object is zero-initialized.

-否则,对象为零初始化。

.

To value-initialize an object of type T means:

值初始化类型为T的对象意味着:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

-如果T是类类型(第9条),具有用户声明的构造函数(12.1),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是不完整的);

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

-如果T是非联合类类型,没有用户声明的构造函数,则T的每个非静态数据成员和基类组件都是值初始化的;

— if T is an array type, then each element is value-initialized;

-如果T是数组类型,则每个元素都初始化值;

— otherwise, the object is zero-initialized

-否则,对象为零初始化

.

Is the value it gets implementation defined or is it the same for all implementations?

是定义了实现的值,还是所有实现的值都相同?

So the value would be same for all implementations.

所有实现的值都是一样的。

Struct is a non-POD type so

Struct是一种非pod类型

 Struct *a =new Struct; // default initialization

 //memberVariable will be initialized to 0 because if T is a non-POD class type
 //the default constructor for T is called 

 Struct *b = new Struct(); //value initializes Struct, which calls the default ctor.

 //memberVariable will be initialized to 0 in this case also.

EDIT :

编辑:

As @Johannes noticed the primitive type (int, bool, float, enum, pointer) member variable is value-initialized not default initialized.

正如@Johannes注意到原始类型(int, bool, float, enum,指针)的成员变量是值初始化而不是默认初始化的。

#3


10  

For primitive types, default initialisation means that the object is initialised with 0, 0.0 or NULL as appropriate for the type.

对于基元类型,默认初始化意味着对象被初始化为0、0或NULL,以适应该类型。

Edit: The above is valid for C++98. In C++03, the terms got redefined a bit. Now, using an initialiser of () (which is syntactically only possible for member-objects) results in value initialisation, which for primitive types means that the appropriate value of 0, 0.0 or NULL gets stored.

编辑:以上内容对c++ 98有效。在c++ 03中,术语被重新定义了一点。现在,使用()的首字母缩写符(在语法上仅适用于成员对象)将导致值初始化,对于基本类型,这意味着存储适当的0、0或NULL值。

#4


3  

0

0

If you call () on a primitive, the effect is the same as assigning the default value it would have been given if it had been static.

如果您调用一个原语,其效果与分配默认值时的效果相同,如果它是静态的。

#5


-1  

It depends on how you instantiate a class, if you use ClassName() the POD classes are default initialized to zero for non POD class default constructor is called but if you use ClassName, without the parentheses no default initialization takes place.

这取决于你如何实例化一个类,如果你使用ClassName(), POD类会被默认初始化为零,而非POD类的默认构造函数会被调用,但是如果你使用ClassName,没有括号,默认初始化就不会发生。

#6


-2  

Native types like int usually get a garbage value, eg. whatever happens to reside in the memory area it is created in. However this is not defined in the standard, and it might also be initialized to 0, which is quite common in eg. debug builds.

本机类型如int通常会得到一个垃圾值,例如。无论发生什么,它都位于创建的内存区域中。然而,这在标准中没有定义,它也可能被初始化为0,这在eg中是很常见的。调试构建。

EDIT. But basically, you should never trust an uninitialized variable to hold something specific; Always define the values yourself.

编辑。但是,基本上,您不应该信任未初始化的变量来保存特定的内容;总是自己定义价值。

#1


30  

You are not correct. The object is not default-initialized but value-initialized. And its value is well-defined

你是不正确的。对象不是默认初始化的,而是值初始化的。它的价值是明确的。

int = 0, 
bool = false, 
float = 0.0f,  
enum = (enum type)0, 
pointer = null pointer
pointer to member = null member pointer

Note that zero is in the range of values for any enumeration, even if it doesn't contain an explicit enumerator with that vaue, so it's safe to initialize an enumeration variable to that value.

注意,0在任何枚举的值范围内,即使它不包含带有vaue的显式枚举数,因此将枚举变量初始化为该值是安全的。

In particular for pointer to data members, the representation used in practice is not all-zero bits. In the so-called C++ Itanium ABI used by at least GCC and Clang, pointer to data members have an all-one bits null representation.

特别是对于指向数据成员的指针,在实践中使用的表示不是全为零的位。在至少由GCC和Clang使用的所谓的c++ Itanium中,指向数据成员的指针具有全- 1位空表示。

#2


27  

The Standard says (8.5/5)

标准的说(8.5/5)

To default-initialize an object of type T means:

默认初始化类型为T的对象意味着:

— if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if Thas no accessible default constructor);

-如果T是一种非pod类类型(第9条),则调用T的默认构造函数(如果没有可访问的默认构造函数,则初始化形式不佳);

— if T is an array type, each element is default-initialized;

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

— otherwise, the object is zero-initialized.

-否则,对象为零初始化。

.

To value-initialize an object of type T means:

值初始化类型为T的对象意味着:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

-如果T是类类型(第9条),具有用户声明的构造函数(12.1),则调用T的默认构造函数(如果T没有可访问的默认构造函数,则初始化是不完整的);

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

-如果T是非联合类类型,没有用户声明的构造函数,则T的每个非静态数据成员和基类组件都是值初始化的;

— if T is an array type, then each element is value-initialized;

-如果T是数组类型,则每个元素都初始化值;

— otherwise, the object is zero-initialized

-否则,对象为零初始化

.

Is the value it gets implementation defined or is it the same for all implementations?

是定义了实现的值,还是所有实现的值都相同?

So the value would be same for all implementations.

所有实现的值都是一样的。

Struct is a non-POD type so

Struct是一种非pod类型

 Struct *a =new Struct; // default initialization

 //memberVariable will be initialized to 0 because if T is a non-POD class type
 //the default constructor for T is called 

 Struct *b = new Struct(); //value initializes Struct, which calls the default ctor.

 //memberVariable will be initialized to 0 in this case also.

EDIT :

编辑:

As @Johannes noticed the primitive type (int, bool, float, enum, pointer) member variable is value-initialized not default initialized.

正如@Johannes注意到原始类型(int, bool, float, enum,指针)的成员变量是值初始化而不是默认初始化的。

#3


10  

For primitive types, default initialisation means that the object is initialised with 0, 0.0 or NULL as appropriate for the type.

对于基元类型,默认初始化意味着对象被初始化为0、0或NULL,以适应该类型。

Edit: The above is valid for C++98. In C++03, the terms got redefined a bit. Now, using an initialiser of () (which is syntactically only possible for member-objects) results in value initialisation, which for primitive types means that the appropriate value of 0, 0.0 or NULL gets stored.

编辑:以上内容对c++ 98有效。在c++ 03中,术语被重新定义了一点。现在,使用()的首字母缩写符(在语法上仅适用于成员对象)将导致值初始化,对于基本类型,这意味着存储适当的0、0或NULL值。

#4


3  

0

0

If you call () on a primitive, the effect is the same as assigning the default value it would have been given if it had been static.

如果您调用一个原语,其效果与分配默认值时的效果相同,如果它是静态的。

#5


-1  

It depends on how you instantiate a class, if you use ClassName() the POD classes are default initialized to zero for non POD class default constructor is called but if you use ClassName, without the parentheses no default initialization takes place.

这取决于你如何实例化一个类,如果你使用ClassName(), POD类会被默认初始化为零,而非POD类的默认构造函数会被调用,但是如果你使用ClassName,没有括号,默认初始化就不会发生。

#6


-2  

Native types like int usually get a garbage value, eg. whatever happens to reside in the memory area it is created in. However this is not defined in the standard, and it might also be initialized to 0, which is quite common in eg. debug builds.

本机类型如int通常会得到一个垃圾值,例如。无论发生什么,它都位于创建的内存区域中。然而,这在标准中没有定义,它也可能被初始化为0,这在eg中是很常见的。调试构建。

EDIT. But basically, you should never trust an uninitialized variable to hold something specific; Always define the values yourself.

编辑。但是,基本上,您不应该信任未初始化的变量来保存特定的内容;总是自己定义价值。