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
- 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)
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):
The {0} is a special initializer that sets pretty much any structure to all zeroes.
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:
that treats
list
as a monodimensional array of 27double
s and fills it with zeroes (more info about that trick here).Notice that this argument is also treated briefly into the array FAQ.
In C++03 you can only use that initialization when defining the array:
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:
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 0Am I missing something or can you not just do
Why not set the values to zero with
memset
or loop ?