简体   繁体   English

在 C++ 中,指针类型数组的元素是否默认保证初始化为 nullptr?

[英]In C++ are elements of an array of pointer type by default guaranteed to be initialized to nullptr?

If I have code like so如果我有这样的代码

class Node {
public:
    Node *subnodes[10];
};
Node x = Node();

is it guaranteed after that code runs that x->subnodes[0] == nullptr ?代码运行后是否保证x->subnodes[0] == nullptr

I'm no expert at C++, and I find C++ specs daunting.我不是 C++ 方面的专家,我发现 C++ 规格令人生畏。 This works out in practice, but is it guaranteed by the spec that nullptr is put in each element of the array in this case (as opposed to, say, potentially garbage)?这在实践中是可行的,但是规范是否保证在这种情况下将 nullptr 放入数组的每个元素中(而不是说,可能是垃圾)? I haven't convinced myself by finding authoritative spec language to that effect so far, and out in the wild I see lots of examples of code seeming not to trust that this is true.到目前为止,我还没有通过找到权威的规范语言来说服自己,并且在野外我看到很多代码示例似乎不相信这是真的。 So, after some time failing to come up with the answer, I turn to you, stack overflow .所以,在一段时间未能提出答案后,我转向你,堆栈溢出 Thanks in advance.提前致谢。

Yes, it is guaranteed.是的,这是有保证的。

Node() constructs a temporary object and performsvalue initialization . Node()构造一个临时的 object 并执行值初始化 As the result, all the elements of the member array subnodes are zero-initialized as null pointer.结果,成员数组subnodes的所有元素都被 零初始化为 null 指针。 x is copy-initialized from the temporary object and its members get the same initialization result too. x是从临时 object 复制初始化的,它的成员也得到相同的初始化结果。 (Because of copy elision x might be value-initialized directly, anyway the result won't change.) (因为复制省略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;如果 T 是 class 类型,其默认构造函数既不是用户提供也不是删除(也就是说,它可能是一个class具有隐式定义或默认的默认构造函数), ZA8CFDE6331BD59EB2AC96F8911C4B66如果它具有非平凡的默认构造函数,则初始化

and

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.如果 T 是标量类型,则对象的初始值是显式转换为 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.如果 T 是非联合 class 类型,则所有基类和非静态数据成员都初始化为零,并且所有填充都初始化为零位。 The constructors, if any, are ignored.构造函数(如果有)将被忽略。
  • ... ...
  • If T is array type, each element is zero-initialized.如果 T 是数组类型,则每个元素都进行零初始化。
  • ... ...

BTW: For default initialization like Node x;顺便说一句:对于像Node x;这样的默认初始化 , the elements of the member array would be initialized to indeterminate values. ,成员数组的元素将被初始化为不确定的值。

While it is guaranteed to intialize it to nullptr, I would not recommend relying on the default intializing in c++.虽然保证将其初始化为 nullptr,但我不建议依赖 c++ 中的默认初始化。 Sometimes the Default intialization does some weird things, for example, if you did not intialize an integer it intializes it with the next available memory address, which makes zero sense.有时默认初始化会做一些奇怪的事情,例如,如果您没有初始化 integer,它会使用下一个可用的 memory 地址对其进行初始化,这是零意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++默认构造函数不初始化指向nullptr的指针? - C++ default constructor does not initialize pointer to nullptr? `thrown nullptr` 必须作为 C++ 中任何完整类型的指针被捕获吗? - Must `thrown nullptr` be caught as a pointer on any complete type in C++? C++ 标准是否保证 std::vector 的底层指针初始化为 nullptr - Does C++ standard guarantee that std::vector's underlying pointer initialized as nullptr C++ - “!pointer”和“pointer == nullptr”之间的区别? - C++ - Difference between "!pointer" and "pointer == nullptr"? 在C ++中将指针数组的所有元素设置为nullptr - Setting all elements of an array of pointers to nullptr in C++ 对C ++空指针使用NULL还是nullptr? - Use NULL or nullptr for a C++ null pointer? C ++将迭代器转换为指针(并将end()转换为nullptr) - C++ convert iterator to pointer (and end() to nullptr) 类ctor中的默认nullptr指针给出未解析的外部符号c ++ - Default nullptr pointer in class ctor give unresolved external symbol c++ C ++如何制作模板 <T> f()返回-1表示积分T,nullptr表示指针类型 - C++ How to make template<T>f() return -1 for integral T, nullptr for pointer-type C ++:设置指向nullptr的指针和将其初始化为新的变量类型之间的区别 - C++: Difference between setting a pointer to nullptr and initializing it as a new variable type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM