Zero initialization of an array data element in the constructor

I have an array of class objects, and inside the class object, I have another array that I need to initialize for all zeros. The code compiles and runs, but C and not 0 is displayed on my output.

From the header file:

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

From cpp file

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

from another 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 configured correctly? As I already mentioned, getBytes returns โ€œCโ€ instead of โ€œ0โ€ as expected.

+6
source share
6 answers

Just use value initialization in the constructor initialization list. This is an idiomatic way to do this in C ++.

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

Note that C ++ 11 also supports this syntax:

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

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

C ++ 11 ยง 8.5, p10

An object whose initializer is an empty set of brackets, i.e. () must be initialized with a value .

This term initializes the meaning of:

C ++ 11 ยง 8.5, p7

To initialize an object of type type T means:

  • if T is a (possibly cv-qualified) class of type 9 with a user-provided constructor (12.1), then the default constructor for T is called (and initialization is bad if T does not have an available default constructor);

  • if T is a (possibly cv-qualified) non-unit class without a constructor provided by the user, then the object is initialized to zero and if TS is an implicitly declared default constructor is nontrivial, then the constructor is called.

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

  • otherwise, the object is initialized to zero.

The third option in this disables the initialization of the value of each element; the fourth applies when we get to each of these elements because they are (a) not types of classes, therefore (1) and (2) have disappeared, and (b) are not arrays, therefore (3) has disappeared. This leaves only the last one, and your elements are initialized to zeros.

+22
source

In the Cache constructor, when you execute:

 byte[16]={0}; 

You only set the 16th byte of your array (which is out of scope, so this operation has undefined behavior). Array objects are initialized by default in C ++, because you store int , no initialization is performed.

You can use std::fill to initialize it:

 Cache::Cache() { std::fill(byte, byte+16, 0); } 

Or you can use a regular loop for your array.

+5
source

memset is the easiest solution.

 Cache::Cache() { memset(byte, 0, sizeof(byte)); } 
+2
source

You are doing it wrong on many levels. The syntax you use does not do what you think. You are now initializing the 17th element of the table to 0.

In your case, memset is probably the fastest and easiest. However, this will not work for complex types, so I would like to write a simple snippet for a general example, for example:

 template<typename T> inline void zero_init(T array[], size_t elements){ if( std::is_pod<T>() ) memset(array, 0, sizeof(T)*elements); else std::fill(begin(array), begin(array)+elements, 0); } 

This will check if the type is a POD type, which in this context means that it can be initialized with memset and will put 0 for the whole table. If T does not support it, then for each element the equivalent of element = 0 will be called. Also, the check can be evaluated at compile time, so if is most likely, if will be compiled, and for each type a simple version with one layer will be created at compile time.

You can call through:

 Cache::Cache() { zero_init(byte, 16); } 
+2
source

There are two problems in the code:

 byte[16]={0}; 

Array has an index based on 0 , so the maximum index in this case can be 15 , not 16 . You corrupt the memory.

Secondly, you need to go through all the elements and initialize them. The initialization method will be performed for only one element.

 Cache::Cache() { for(int i=0;i<16;i++) byte[i]=0; } 
+1
source

Initializing an array with {0} only works after declaring the array.

You need a for loop inside the constructor, which sets the elements of the array to zero.

0
source

Source: https://habr.com/ru/post/970155/


All Articles