g++ warning on declaring multidimensional, double array

青春壹個敷衍的年華 提交于 2019-12-20 04:12:45

问题


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

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

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

Hence I manually initialized this array to all 0's 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 got the compiler warning:

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

My question is therefore

  1. Why does list have non-zero entries after being initialized in the header?
  2. Why do I get 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:


Am I missing something or can you not just do

class Class {
public:
    double array[3][3][3];

    Class() : array() { }
};



回答2:


In C++03 you can only use that initialization when defining the 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 is warning that according to the current standard it should not accept your code, even if it is able to process it by applying the upcoming standard rules, where the {...} is called an external-initializer.

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

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

For members of POD types (which an array of double is), the syntax above (list()) in the initializer list means that the member is to be value-initialized which effectively means that all values will be set to 0




回答3:


  1. In the header it's not initialized, it's just declared. Builtin types (and more in general PODs) are not automatically zero-initialized if they aren't globals (actually, that have static storage duration) for performance reasons (as many things in C++, initialization is an opt-in, you pay for it only if you need it).

  2. That syntax is valid only when initializing an array, while that instruction is an assignment. That syntax, instead can be used in many more places in the new C++ standard (formerly C++0x, now C++11 since it has just been approved).

In my opinion, the quickest way to solve your problem is just to do:

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

that treats list as a monodimensional array of 27 doubles and fills it with zeroes (more info about that trick here).

Notice that this argument is also treated briefly into the array FAQ.




回答4:


There's a simpler way to do it. The following should work in C or C++, just for initializing the variable (i.e. not in an assignment):

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

The {0} is a special initializer that sets pretty much any structure to all zeroes.




回答5:


Why not set the values to zero with memset or loop ?



来源:https://stackoverflow.com/questions/7164689/g-warning-on-declaring-multidimensional-double-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!