C ++零初始化 [英] C++ Zero-Initialization
问题描述
我无法理解何时以及为什么根据 http://zh.cppreference.com/w/cpp/language/zero_initialization 。
I'm having trouble understanding when and why exactly a member in my class is zero-initialized according to http://en.cppreference.com/w/cpp/language/zero_initialization.
请考虑以下测试程序:
#include <iostream>
#include <stdio.h>
class MyTest {
private:
const static unsigned int dimension = 8;
void (* myFunctions [dimension])();
public:
MyTest() {}
void print() {
for(unsigned int i=0; i < MyTest::dimension; i++) {
printf("myFunctions[%d] = %p\n", i, this->myFunctions[i]);
}
}
};
int main() {
//We declare and initialize an object on the stack
MyTest testObj = {};
testObj.print();
return 0;
}
我声明一个类具有签名的8个函数指针的数组 void functionname()。当我在 main
中声明并初始化该类的对象为 MyTest testObj = {};
或 MyTest testObj;
,我希望它会被零初始化,即所有指针都是空指针。
I am declaring a class to have an array of 8 function pointers of the signature "void functionname()". When I declare and initialize an object of the class in main
as MyTest testObj = {};
or MyTest testObj;
, I expected it to be zero-initialized, i.e. all pointers are null pointers.
但是,使用g ++ 5.3编译在Windows 10机器上的<.c上使用 g ++ -m32 -o test -std = c ++ 14 test.cpp&&测试
机器给出输出:
However, compiling with g++ 5.3.0 on my Windows 10 machine with g++ -m32 -o test -std=c++14 test.cpp && test
machine gives the output:
myFunctions[0] = 76dd6b7d
myFunctions[1] = 00401950
myFunctions[2] = 0061ff94
myFunctions[3] = 004019ab
myFunctions[4] = 00401950
myFunctions[5] = 00000000
myFunctions[6] = 003cf000
myFunctions[7] = 00400080
哪个看起来像是未初始化的值
Which look like un-initialized values from the stack..
如果我将对象的声明移到main之外(作为全局变量),它将再次打印所有零。
If I move the declaration of the object outside of main (as a global variable), it prints all zeroes again.
如果我正确地理解了cppreference,这是因为我有一个带有静态存储持续时间的变量,因此被初始化为零。它通过零初始化类的所有非静态数据成员(即 myFunctions
)数组来初始化我的类类型。通过对数组的每个元素进行零初始化来初始化数组,在我的函数指针情况下,该数组为空指针。
If I have understood cppreference correctly, this is because I have a variariable with static storage duration, and is thus zero-initialized. It initializes my class type by zero-initializing all non-static data members of my class (i.e., the myFunctions
) array. An array is initialized by zero-initializing every element of it, which, in my function pointer case, is a null pointer.
为什么不对它进行零初始化我用 MyTest testObj = {};
声明堆栈时?
Why does it not zero-initialize my object the stack when I declare it with MyTest testObj = {};
?
推荐答案
以下
MyTest testObj = {};
对于 MyTest <<>不是零初始化
,但只是调用其默认构造函数。 cppreference 页解释了为什么(强调我的意思):
is not zero-initialization for MyTest
, but is simply calling its default constructor. The cppreference page explains why (emphasis mine):
作为值初始化序列的一部分非类类型以及没有构造函数的值初始化类类型的成员,包括未提供初始化程序的聚合元素的值初始化。
As part of value-initialization sequence for non-class types and for members of value-initialized class types that have no constructors, including value initialization of elements of aggregates for which no initializers are provided.
MyTest
是类类型,并且a具有构造函数。
MyTest
is a class type, and a has a constructor.
使用
MyTest() = default;
会将对象初始化为零。
will instead zero-initialize the object.
下面的相关标准报价(强调我的内容)。
Relevant Standard quotes (emphasis mine) below.
来自 [dcl.init#8] :
到值初始化类型为T的对象的意思是:
To value-initialize an object of type T means:
-
如果T是(可能是cv限定的)类类型,或者没有默认构造函数([class.ctor])或用户提供或删除的默认构造函数,则该对象将被默认初始化;
if T is a (possibly cv-qualified) class type with either no default constructor ([class.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;
如果T是(可能是cv限定)的类类型,而没有用户提供或删除的默认构造函数,则该对象将被初始化为零,并检查默认初始化的语义约束,如果T具有非平凡的默认构造函数,则该对象将被默认初始化;
if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;
...
来自 [dcl.init.list] :
对象或类型T的引用的列表初始化的定义如下:
List-initialization of an object or reference of type T is defined as follows:
-
...
...
否则,如果初始化列表没有元素,且T为
这篇关于C ++零初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!