在 C++ 中默认初始化的原始类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3803153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are primitive types default-initialized to in C++?
提问by sharptooth
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
, 指针) 成员变量是default-initialied。它获得实现定义的值还是所有实现都相同?
采纳答案by Johannes Schaub - litb
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.
请注意,零在任何枚举的值范围内,即使它不包含具有该值的显式枚举器,因此将枚举变量初始化为该值是安全的。
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 ABI 中,指向数据成员的指针具有全 1 位空表示。
回答by Prasoon Saurav
The Standard says (8.5/5
)
标准说 ( 8.5/5
)
To default-initialize an object of type T means:
— 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);
— if T is an array type, each element is default-initialized;
— otherwise, the object is zero-initialized.
默认初始化 T 类型的对象意味着:
— 如果 T 是非 POD 类类型(第 9 条),则调用 T 的默认构造函数(如果没有可访问的默认构造函数,则初始化是格式错误的);
— 如果 T 是数组类型,则每个元素都默认初始化;
— 否则,对象是零初始化的。
.
.
To value-initialize an object of type T means:
— 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);
— 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;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized
对 T 类型的对象进行值初始化意味着:
— 如果 T 是具有用户声明的构造函数 (12.1) 的类类型(第 9 条),则调用 T 的默认构造函数(如果 T 没有可访问的默认构造函数,则初始化是格式错误的);
— 如果 T 是没有用户声明的构造函数的非联合类类型,则 T 的每个非静态数据成员和基类组件都进行了值初始化;
— 如果 T 是数组类型,则每个元素都进行了值初始化;
— 否则,对象是零初始化的
.
.
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、pointer)成员变量value-initialized
不是default initialized
.
回答by Bart van Ingen Schenau
For primitive types, default initialisationmeans that the object is initialised with 0, 0.0 or NULL as appropriate for the type.
对于原始类型,默认初始化意味着对象根据类型使用 0、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 中,这些术语被重新定义了一点。现在,使用 of 的初始化器()
(在语法上仅适用于成员对象)会导致值初始化,这对于原始类型意味着存储 0、0.0 或 NULL 的适当值。
回答by Jon Hanna
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.
如果你调用()
一个原语,效果与分配默认值相同,如果它是静态的,它会被赋予。
回答by yesraaj
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,没有括号则不会进行默认初始化。
回答by reko_t
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 这样的本地类型通常会得到一个garbage value
,例如。无论发生在它被创建的内存区域中的任何东西。然而,这在标准中没有定义,它也可能被初始化为 0,这在例如很常见。调试构建。
EDIT. But basically, you should never trust an uninitialized variable to hold something specific; Always define the values yourself.
编辑。但基本上,你永远不应该相信一个未初始化的变量来保存特定的东西;始终自己定义价值观。