Initializing a Dynamically Allocated Array . Why doesn't it work?!

C++ Primer says:
We can value-initialize the elements by following the array size by an empty pair of parentheses:
int *pia2 = new int[10] (); // array of 10 uninitialized ints
The parentheses are effectively a request to the compiler to value-initialize the array, which in this case sets its elements to 0.


so I try the following codes:
1
2
int *pia2 = new int[10] ();
cout << pia2[0] << endl << pia2[1] << endl;


Then it prints out:
3351280
3342588


Why??? the elements are still uninitialized??!

btw: I use Dev-c++, I'm wondering if this is the cause.
Line one should have '()' at the end.

And that output is correct. Just because an array, or variable, does not get initialized to a specific value, does no tmean it doesn't contain a value; it is just undefinded.
Last edited on
btw: I use Dev-c++, I'm wondering if this is the cause.


While it still is not being maintained anymore it only has a few bugs I know of however that is reason enough. switch to codeblocks, wxWidgets, Eclipse, even VSC++ would be better :P
Topic archived. No new replies allowed.