Is new int[10]() valid c++? Is new int[10]() valid c++? arrays arrays

Is new int[10]() valid c++?


First of all is this valid C++ or is it a microsoft extension?

It is valid in C++, the relevant part of the standard is 5.3.4, with the first paragraph containing the grammar

Is it guaranteed to initialize all the elements of the array?

Yes. Paragraph 5.3.4/15 states that

A new-expression that creates an object of type T initializes that object as follows:

...

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

where value initialized for POD means zero-initialize.

Also, is there any difference if I do new int; or new int();? Does the latter guarantee to initialize the variable?

Yes they are different. According with the quote above new int() will zero-initialize the integer. In a previous block of the same paragraph:

If the new-initializer is omitted:

  • If T is a (possibly cv-qualified) non-POD class type (or array thereof), the object is default-initialized (8.5). If T is a const-qualified type, the underlying class type shall have a user-declared default constructor.

  • Otherwise, the object created has indeterminate value. If T is a const-qualified type, or a (possibly cv-qualified) POD class type (or array thereof) containing (directly or indirectly) a member of const-qualified type, the program is ill-formed;

so new int will not initialize the memory.


From the standard. "To default-initialize an object of type T means: -- if T is a non-POD class type [the case like vector], the default constructor for T is called."

The constructor notation T() is used to express the default value of type T and that this is zero for built in types and the default constructor for user-defined types. The construct POD() produces value initialisation and according to the stdandard, which results in all members and sub members being either default constructed or zero initialised.

This is why your statement is legal, but is it zero initialized as per the standard; needs some detailed lookup IMHO.

BUT, i could not find in the standard where the it defines the meaning of default construction for built in types.

EDIT:-

struct S { int x; };void f () {   S s1;       // s1.x is uninitialized here   S s2 = S(); // s2.x is zero here}

I think we often make up our own interpretation of what default construction applied to built in types means; because the C++ standard doesn't define it (atleast i could not find it) and i remember Stroustrup or Josuttis say that it means T() which is described as being value initialisation is "zero converted to type T" for built in types.

Since int* is a built-in type it is initialized to zero.

But i am really not sure.


  1. This is valid C++ according to paragraph 5.3.4/1, which is hard to quote here due to special formatting.

  2. According to 5.3.4/15 the item is value-initialized if form () is used, see paragraph 8.5 but the short answer is yes.

  3. Yes there is a difference in the first case the variable is not value-initalized and can be anything, in the second case it's value initialized and will be zero.