g++ warning on declaring multidimensional, double array
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 compile开发者_如何学Cr warning:
warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
My question is therefore
- Why does list have non-zero entries after being initialized in the header?
- 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)
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
Am I missing something or can you not just do
class Class {
public:
double array[3][3][3];
Class() : array() { }
};
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).
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 double
s and fills it with zeroes (more info about that trick here).
Notice that this argument is also treated briefly into the array FAQ.
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.
Why not set the values to zero with memset
or loop ?
精彩评论