Reputation:
It is well known that missing initializers for an array of scalars are defaulted to zero.
int A[5]; // Entries remain uninitialized
int B[5]= { 0 }; // All entries set to zero
But is this (below) guaranteed ?
int C[5]= { }; // All entries set to zero
Upvotes: 97
Views: 121557
Reputation: 1252
At first, let's prove why every element in the below array A should be zero
int A[5] = {0};
How to guaranteed? It involves the below standards:
Each direct public base, (since C++17) array element, or non-static class member, in order of array subscript/appearance in the class definition, is copy-initialized from the corresponding clause of the initializer list.
If the number of initializer clauses is less than the number of members and bases (since C++17) or initializer list is completely empty, the remaining members and bases (since C++17) are initialized by their default member initializers, if provided in the class definition, and otherwise (since C++14) copy-initialized from empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates). If a member of a reference type is one of these remaining members, the program is ill-formed.
otherwise, the object is zero-initialized.
If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.
Proof done!
Finally, let's prove why every element in the below array B should be zero
int B[5] = {};
It is a value-initialization form, according to Value Initialization
In all cases, if the empty pair of braces {} is used and T is an aggregate type, aggregate-initialization is performed instead of value-initialization.
It will go into upper aggregate-initialization #2, and then every element in B array should be zero.
Proof done!
Upvotes: 0
Reputation: 794
In fact when you sayint A[5] = { 0 };
you are saying: Initialize the first element to zero. All the other positions are initialized to zero because of the aggregate inizialization.
This line is the real responsible for having your array full of zeroes: int A[5] = { };
That is why if you use int A[5] = { 1 };
you will only have the first position inizialized to 1.
Upvotes: 17
Reputation: 172924
Yes, according to the rule of aggregate initialization, it's guaranteed (that all elements of array C
will be value-initialized, i.e. zero-initialized to 0
in this case).
(emphasis mine)
If the number of initializer clauses is less than the number of members
and bases (since C++17)
or initializer list is completely empty, the remaining membersand bases (since C++17)
are initializedby their default initializers, if provided in the class definition, and otherwise (since C++14)
by empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates).
PS:
int A[5]; // Entries remain uninitialized
"remain uninitialized" might not be accurate. For int A[5];
, all elements of A
will be default-initialized. If A
is static or thread-local object, the elements will be zero-initialized to 0
, otherwise nothing is done, they'll be indeterminate values.
Upvotes: 38
Reputation: 234715
The empty braced initialisation performs aggregation-initialization of the array: this leads to zero-initialization of the int
elements.
Yes, this is guaranteed.
Upvotes: 114