What is the difference between int* arr = new int [5];
and int* arr = new int();
?
int* arr = new int [5];
The above allocates an array of 5 int
without initialising them, and assigns it to the new variable arr
. The array should be freed using delete [] arr;
.
int* arr = new int();
The above allocates a single value-initialized int
, and assigns it to the mis-named new variable arr
. The memory should be freed using delete arr;
.
Accessing out-of-bounds, or trying to free something the wrong way all causes Undefined Behavior, which means neither compiler nor runtime are under any requirements whatsoever.
As your program ends shortly thereafter, it's acceptable to leak those allocations to avoid the make-work. You should add a comment that you do so intentionally though.