user1196549
user1196549

Reputation:

Initializing an array of zeroes

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

Answers (4)

ravin.wang
ravin.wang

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:

  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.

  1. 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.

  1. According to Value Initialization, the left 4 elements will routine into 'zero initialization' process

otherwise, the object is zero-initialized.

  1. 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.

  1. 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!

Upvotes: 0

rain_
rain_

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

songyuanyao
songyuanyao

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 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.

Upvotes: 38

Bathsheba
Bathsheba

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

Related Questions