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:
- First of all is this valid C++ or is
it a microsoft extension? - Is it guaranteed to initialize all
the elements of the array? - Also, is there any difference if I
donew int;ornew int();? Does
the latter guarantee to initialize
the variable?
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
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:so
new intwill not initialize the memory.