If the allocation was made with new <type>[n]
then the deallocation must be made with delete []
.
All that counts when determining the correct form of delete
is the form of new
that was used in the allocation.
In your main
function, you leak memory. You initialize arr1
with a call to new
and then immediately overwrite that value with the new pointer returned by the call to f()
. The call to new
from main
is simply wrong and should be removed. Write it simply like this:
int* arr1 = f(n);
And your main
should be
int main()