Problem with new. Please help.
I compiled the following code using 3 different compiler. Each gave me different result.
Code:
#include <iostream>
#include <cstddef>
using std::cout;
using std::endl;
using std::cin;
int main(void)
{
int *a = new int [10] ();
for ( size_t i = 0; i < 10; i++ ) {
cout << a[i] << ' ';
}
cout << endl;
cin.get();
return 0;
}
When using MSVC++6, the program displays:
Quote:
-842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451
When using Dev-C++4, the program displays:
Quote:
2557144 2557144 0 0 0 0 0 0 0 0
And when using g++3.2, the program displays:
Quote:
0 0 0 0 0 0 0 0 0 0
which is what I'm expecting.
The following quoted sentences are from C++ Primer, 4th Edition
Quote:
we can value-initialize the elements by following the array size by an empty pair of parentheses:
int *pia2 = new int[10] ();
The parentheses are effectively a request to the compiler to value-initialize the array, which in this case sets its elements to 0.
Does these sentences mean that it's in standard that each element in the dynamically allocated array should be initialized to 0?
Is it because msvc++6 and dev-c++ are too old to support this newly standard? Or whether the elements are initialized to 0 or not is implementation-dependent?