views:

420

answers:

3
int * array = new int [size]();

The operator () allow to set all values of array to 0 (all bits to 0). it's called value-initialization.

Since which version of g++ is it valid ?

What about other compilers ?

Where can i find it in standard ?

Thank you for your future anwsers.

A: 
Nikolai N Fetissov
+6  A: 

This is part of the C++ standard; if it was invalid in g++ then g++ was nonconforming. From the C++ standard (ISO/IEC 14882:2003), several sections are relevant:

5.3.4/15 concerning the new expression says:

If the new-initializer is of the form (), the item is value-initialized

8.5/5 concerning initializers says:

To value-initialize an object of type T means:

— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

— if T is an array type, then each element is value-initialized;

— otherwise, the object is zero-initialized

So, for an array of ints, which are a scalar type, the third and fourth bullet points apply.

James McNellis
Thank you ! Since which version of g++ is it valid ?
Nadir SOUALEM
+1  A: 

Initialization with () (including your example) was always a part of standard C++, since C++98. Although there were some changes in the newer versions of the standard, they don't apply to your example.

GCC compilers were known to handle () initializers incorrectly in versions from 2.x.x family. MSVC++ compiler is known to handle () initializers incorrectly in VC6. Newer versions of MSVC++ handle () initializers in accordance with C++98 specification.

AndreyT
Have you sources ? FOr g++ 3.4.3 it seems to fail.
Nadir SOUALEM
Hmm.. Works fine in my 3.4.4. How exactly does it fail in 3.4.3? Garbage in the array? Refuses to compile?
AndreyT
Sorry, its gcc 3.4.6 20060404 (Red Hat 3.4.6-9). It compiles fine,when i check the elements, array[0], array[1], etc ... they are differents of zero !!!!
Nadir SOUALEM
I have no explanation for that. Again, it works fine in my 3.4.4. Are you sure you didn't forget the `()` bit in your experiment?
AndreyT
im sure !!! Thats why im asking this question !!!!
Nadir SOUALEM