g++ warning on declaring multidimensional, double

2019-02-28 11:14发布

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)

5条回答
看我几分像从前
2楼-- · 2019-02-28 11:21

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.

查看更多
淡お忘
3楼-- · 2019-02-28 11:28
  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.

查看更多
forever°为你锁心
4楼-- · 2019-02-28 11:31

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

查看更多
Bombasti
5楼-- · 2019-02-28 11:32

Am I missing something or can you not just do

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

    Class() : array() { }
};
查看更多
神经病院院长
6楼-- · 2019-02-28 11:35

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

查看更多
登录 后发表回答