Does new[] call default constructor in C++?

flashnik picture flashnik · Aug 26, 2010 · Viewed 20.8k times · Source

When I use new[] to create an array of my classes:

int count = 10;
A *arr = new A[count];

I see that it calls a default constructor of A count times. As a result arr has count initialized objects of type A. But if I use the same thing to construct an int array:

int *arr2 = new int[count];

it is not initialized. All values are something like -842150451 though default constructor of int assignes its value to 0.

Why is there so different behavior? Does a default constructor not called only for built-in types?

Answer

sharptooth picture sharptooth · Aug 26, 2010

See the accepted answer to a very similar question. When you use new[] each element is initialized by the default constructor except when the type is a built-in type. Built-in types are left unitialized by default.

To have built-in type array default-initialized use

new int[size]();