Search code examples
c++dynamic-memory-allocationnew-operator

New Operator with and without Parentheses


What is the main difference between using the new operator to create an array with trailing parentheses and without? That is, the difference between the following declarations

void* ptr = new int[5]();

and

void* ptr = new int[5];

Solution

  • Details of the new expression are explained here. In your example there is

    new type new-initializer
    

    The type is an array of 5 integers in both cases. The () is the new-initializer. Details of initialization are explained in this section. For integers default initialization (no ()) means the elements are not initialized and value initialization means the elements are initialized with a 0 value.