Initalize an array in constructor

This is a discussion on Initalize an array in constructor within the C++ Programming forums, part of the General Programming Boards category; Hey guys the below code works fine and i get the correct result but I
fear I am coding dangerously ...

Initalize an array in constructor

Hey guys the below code works fine and i get the correct result but I
fear I am coding dangerously here. Its usually better to have the
constructor inialize the array elements to zero before I enter the values
but when i tried this:

Code:

m_Array1[ m_ARRAY_SIZE ] = { 0 }

Inside the constructoir I got an error. Would it be a better idea to have the
constructor call a seperate function that does the above?

To value-initialize an object of type T means:
...
- if T is an array type, then each element is value-initialized;
- otherwise, the object is zero-initialized

...

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

Then:

Originally Posted by Section 12.6 of the C++ Standard (2003)

When no initializer is specified for an object of (possibly cv-qualified) class type (or array thereof), or the initializer has the form (), the object is initialized as specified in 8.5. The object is default-initialized if there is no initializer, or value-initialized if the initializer is ().

My interpretation is that ": m_Array1(), m_Array2()" result in both arrays being value initialised. As such, each int in the respective array is zero-initialised. This works on both MSVC8 (VS2005) and the MinGW port of g++ 3.4.2.

EDIT:
Just came back from my late night shower, and I think I spotted a mistake: 12.6.0 is concerned with the initialisation of the object, not its members. The correct quote is:

Originally Posted by Section 12.6.2 of the C++ Standard (2003

In the definition of a constructor for a class, initializers for direct and virtual base subobjects and nonstatic data members can be specified by a ctor-initializer, which has the form ctor-initializer:

The expression-list in a mem-initializer is used to initialize the base class or nonstatic data member subobject denoted by the mem-initializer-id. The semantics of a mem-initializer are as follows:
- if the expression-list of the mem-initializer is omitted, the base class or member subobject is value initialized (see 8.5);

To confirm that excluding m_Array1 and m_Array2 from the constructor initialisation list should leave their elements uninitialised:

Originally Posted by Section 12.6.2 of the C++ Standard (2003)

If a given nonstatic data member or base class is not named by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer), then
- If the entity is a nonstatic data member of (possibly cv-qualified) class type (or array thereof) or a base class, and the entity class is a non-POD class, the entity is default-initialized (8.5). If the entity is a nonstatic data member of a const-qualified type, the entity class shall have a user-declared default constructor.
- Otherwise, the entity is not initialized. If the entity is of const-qualified type or reference type, or of a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of a const-qualified type, the program is ill-formed.

If this is part of the standard then I should theoretically be able to do this on any new compiler?

Also this means that by providing an array in the intializer list for member arrays I would be essentially forcing the compiler to do a memset(array,0,array_size) correct? I'm not so sure I would want to do that in all instances.

If this is part of the standard then I should theoretically be able to do this on any new compiler?

... that conforms to the C++ Standard with respect to this particular language feature. Considering that Microsoft changed the compiler behaviour to have this new behaviour, and that g++ implements this new behaviour, I would hazard a guess that previous versions of Microsoft's compilers, even MSVC7.1, does not conform to the standard in this respect.

Well the MSDN docs say that the arrays would not always be initialized. Perhaps this was a bug they never got around to fixing in previous versions? But this would mean that relying on such a mechanism in earlier MS compilers would be relying on undefined compiler behavior.

VC++ v7/C++ and VC++ v7.1/C++: no warnings or errorsVC++ v6/C++ and eVC++ v4.0/C++: compile fails with a multitude of errorsEDG/C++, EDG/Abridged, and EDG/C99: no warnings or errorsMinGW/C++: no warnings or errors

I didn't include EDG/EC++ because it can't even find the std:: namespace . . .

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell