Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zero-initializing an array data member in a constructor

I've got an array of class objects, and inside the class object I've got another array that I'd need to initialize to all zeros. The code compiles and runs, but my output is showing C rather than 0.

From the header file:

class Cache {
private:
    int byte[16];
public:
    Cache();
    int getBytes(int);
    ~Cache();
};

From the cpp file

Cache::Cache()  
{
    byte[16]={0};
}

int Cache::getBytes(int j){
    return byte[j];
}

from the other cpp file

for (int i = 0; i < 16; i++) 
{
    for (int j = 0; j < 16; j++)  //visual check of initializes main memory
    {
        cout << cache[i].getBytes(j) << " ";
}
}

Is this being set up correctly? As I mentioned, getBytes is returning 'C's rather than '0's as expected.

like image 452
Anik Avatar asked Jun 02 '14 04:06

Anik


People also ask

Can an array be initialized in a constructor?

You can initialize the array variable which is declared inside the class just like any other value, either using constructor or, using the setter method.

How do you initialize an array with zero values?

The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value. This is used only with GCC compilers.

How do you initialize a data member in a constructor?

Const member variables must be initialized. A member initialization list can also be used to initialize members that are classes. When variable b is constructed, the B(int) constructor is called with value 5. Before the body of the constructor executes, m_a is initialized, calling the A(int) constructor with value 4.

Can you initialize in a constructor?

A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.


1 Answers

Just use value initialization in the constructor initialization list. That is the idiomatic way of doing this in C++.

Cache::Cache() : byte()
{ 
}

Note that C++11 allows this syntax too:

Cache::Cache() : byte{}
{ 
}

In case you're wondering why this works, from the C++ 11 standard (note this also applies to C++03):

C++11 § 8.5,p10

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

That term value-initialized takes us to:

C++11 § 8.5,p7

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type9 with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

  • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.

  • if T is an array type, then each element is value-initialized;

  • otherwise, the object is zero-initialized.

The third option in this trips the value-initialization of each element; the fourth applies once we get to each of those elements because they're (a) no class types, so (1) and (2) are gone, and (b) not arrays, so (3) is gone. That leaves only the last one, and your elements are zero-initialized.

like image 64
juanchopanza Avatar answered Sep 22 '22 13:09

juanchopanza