Initializing an array of zeroes

C++ArraysInitializationLanguage Lawyer

C++ Problem Overview


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

C++ Solutions


Solution 1 - C++

The empty braced initialisation performs aggregation-initialization of the array: this leads to zero-initialization of the int elements.

Yes, this is guaranteed.

Solution 2 - C++

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 members and bases (since C++17) are initialized by 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.

Solution 3 - C++

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.

Solution 4 - C++

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:

  1. It is a Aggregate initialization since it is array type, and its number of initializer clauses is less than the number of members, and the first element is copy-initialized as 0. > 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.

  2. The remaining elements in the array will follow the rule of Aggregate initialization rule, and do value-initialization > 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.

  3. According to Value Initialization, the left 4 elements will routine into 'zero initialization' process > otherwise, the object is zero-initialized.

  4. According to Zero Initialization, the type of remaining elements is int which is a scaler type, so the left 4 elements will be initialized as 0 > If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.

  5. So every element in A should be zero

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYves DaoustView Question on Stackoverflow
Solution 1 - C++BathshebaView Answer on Stackoverflow
Solution 2 - C++songyuanyaoView Answer on Stackoverflow
Solution 3 - C++rain_View Answer on Stackoverflow
Solution 4 - C++ravin.wangView Answer on Stackoverflow