G ++ warning when declaring a multidimensional, double array

In my C ++ program, I am trying to initialize a 3 * 3 * 3 array of type double with all 0. In the class header file, I declared a member

double list[3][3][3]; 

When I printed the contents of this array, I found that not all entries are 0, as I expected. for example list [1] [1] [1] has the value 4.03158e-321

Therefore, I manually initialized this array to all 0 in the constructor:

 list = {{{0,0,0},{0,0,0},{0,0,0}}, {{0,0,0},{0,0,0},{0,0,0}}, {{0,0,0},{0,0,0},{0,0,0}}}; 

This makes my program work, however I received a compiler warning:

 warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x 

Therefore my question

  • Why the list has non-zero entries after initialization to the header
  • Why am I getting the above warning message and how to get rid of it?

My compiler is g ++ (Ubuntu / Linaro 4.5.2-8ubuntu4) 4.5.2, with gcc version 4.5.2 (Ubuntu / Linaro 4.5.2-8ubuntu4)

+1
source share
5 answers

I missed something or I can not just do

 class Class { public: double array[3][3][3]; Class() : array() { } }; 
+3
source

In C ++ 03, you can use this initialization only when defining an array:

 double list[3][3][3] = {{{0,0,0},{0,0,0},{0,0,0}}, {{0,0,0},{0,0,0},{0,0,0}}, {{0,0,0},{0,0,0},{0,0,0}}}; 

The compiler warns that, in accordance with the current standard, it should not accept your code, even if it is able to process it using the upcoming standard rules, where {...} is called an external initializer.

In this particular case, when the array is a member and you want to initialize it to all zeros, you can just use initialization initialization in C ++ 0x:

 struct test { double list[3][3][3]; test() : list() {} }; 

For members of POD types (which have an array of double ), the syntax above ( list() ) in the initializer list means that the element must be initialized with a value, which in fact means that all values ​​will be set to 0

+5
source
  • It is not initialized in the header; it is simply declared. Built-in types (and more general PODs) are not automatically initialized to zero unless they are global (in fact, which have a static storage duration) for performance reasons (since many things in C ++, initialization is a failure, you pay for it only if you need it).

  • This syntax is valid only when initializing the array, whereas this command is the destination. Instead, this syntax can be used in many other places in the new C ++ standard (formerly C ++ 0x, now C ++ 11 since it has just been approved).

In my opinion, the fastest way to solve your problem is simply:

 double * firstElem=&list[0][0][0]; std::fill(firstElem, firstElem+27, 0.); 

which treats list as a monomeric array of 27 double and fills it with zeros (more on this trick here ).

Note that this argument is also briefly discussed in the FAQ array .

+2
source

There is an easier way to do this. The following should work in C or C ++, only to initialize a variable (i.e. Not in the destination):

 double list[3][3][3] = {0}; 

{0} is a special initializer that sets up almost any structure for all zeros.

+1
source

Why not set the values ​​to zero using memset or loop?

0
source

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


All Articles