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?
I'm no expert at C++, and I find C++ specs daunting. 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)? 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, stackoverflow. Thanks in advance.
Yes, it is guaranteed.
Node()constructs a temporary object and performs value initialization. As the result, all the elements of the member arraysubnodesare zero-initialized as null pointer.xis copy-initialized from the temporary object and its members get the same initialization result too. (Because of copy elisionxmight be value-initialized directly, anyway the result won't change.)and
BTW: For default initialization like
Node x;, the elements of the member array would be initialized to indeterminate values.