在 C++ 中,指针类型数组的元素是否默认保证被初始化为 nullptr?
In C++ are elements of an array of pointer type by default guaranteed to be initialized to nullptr?
如果我有这样的代码
class Node {
public:
Node *subnodes[10];
};
Node x = Node();
代码运行后是否保证x->subnodes[0] == nullptr
?
我不是 C++ 专家,我发现 C++ 规范令人望而生畏。这在实践中可行,但规范是否保证在这种情况下将 nullptr 放入数组的每个元素中(而不是说,可能是垃圾)?到目前为止,我还没有通过找到权威的规范语言来说服自己,而且在野外我看到很多代码示例似乎不相信这是真的。所以,在一段时间没有找到答案后,我求助于你,stackoverflow。提前致谢。
是的,有保证。
Node()
构造一个临时对象并执行 value initialization. As the result, all the elements of the member array subnodes
are zero-initialized as null pointer. x
is copy-initialized from the temporary object and its members get the same initialization result too. (Because of copy elision x
可能会直接进行值初始化,反正结果不会改变。)
if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;
和
The effects of zero initialization are:
- If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.
- If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to
zero bits. The constructors, if any, are ignored.
- ...
- If T is array type, each element is zero-initialized.
- ...
顺便说一句:对于像 Node x;
这样的 default initialization,成员数组的元素将被初始化为不确定的值。
虽然保证将其初始化为 nullptr,但我不建议依赖 c++ 中的默认初始化。有时默认初始化会做一些奇怪的事情,例如,如果你没有初始化一个整数,它会用下一个可用的内存地址初始化它,这是零意义。
如果我有这样的代码
class Node {
public:
Node *subnodes[10];
};
Node x = Node();
代码运行后是否保证x->subnodes[0] == nullptr
?
我不是 C++ 专家,我发现 C++ 规范令人望而生畏。这在实践中可行,但规范是否保证在这种情况下将 nullptr 放入数组的每个元素中(而不是说,可能是垃圾)?到目前为止,我还没有通过找到权威的规范语言来说服自己,而且在野外我看到很多代码示例似乎不相信这是真的。所以,在一段时间没有找到答案后,我求助于你,stackoverflow。提前致谢。
是的,有保证。
Node()
构造一个临时对象并执行 value initialization. As the result, all the elements of the member array subnodes
are zero-initialized as null pointer. x
is copy-initialized from the temporary object and its members get the same initialization result too. (Because of copy elision x
可能会直接进行值初始化,反正结果不会改变。)
if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;
和
The effects of zero initialization are:
- If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.
- If T is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.
- ...
- If T is array type, each element is zero-initialized.
- ...
顺便说一句:对于像 Node x;
这样的 default initialization,成员数组的元素将被初始化为不确定的值。
虽然保证将其初始化为 nullptr,但我不建议依赖 c++ 中的默认初始化。有时默认初始化会做一些奇怪的事情,例如,如果你没有初始化一个整数,它会用下一个可用的内存地址初始化它,这是零意义。