I compiled the following code using 3 different compiler. Each gave me different result.
When using MSVC++6, the program displays: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 Dev-C++4, the program displays:-842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451 -842150451
And when using g++3.2, the program displays:2557144 2557144 0 0 0 0 0 0 0 0
which is what I'm expecting.0 0 0 0 0 0 0 0 0 0
The following quoted sentences are from C++ Primer, 4th Edition
Does these sentences mean that it's in standard that each element in the dynamically allocated array should be initialized to 0?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.
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?



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.
CornedBee