Reputation: 73473
While trying to answer this question I found that the code int* p = new int[10]();
compiles fine with VC9 compiler and initializes the integers to 0. So my questions are:
new int;
or new int();
? Does
the latter guarantee to initialize
the variable?Upvotes: 26
Views: 5400
Reputation: 7190
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.
Upvotes: 2
Reputation: 52549
This is valid C++ according to paragraph 5.3.4/1, which is hard to quote here due to special formatting.
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.
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.
Upvotes: 1
Reputation: 208406
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.
Upvotes: 29
Reputation: 25690
If I may guesstimate a bit (I'm sure I'll be corrected if I'm wrong):
The latter () does value-initialization (as it's called in standardese) which makes the integer have value 0 in this case.
That is a rather new addition to the standard (C++03?) so older compilers may not support it, and leave it uniintialized.
I recall getting a lot of warnings w.r.t this when switching from MSVS 2003 to 2005 (I think), where the compiler said that "this member will be zero-initialized now, it wasn't before".
Upvotes: 0