If we allocate an object of size 1 as below
int *arr = new int[1];
Should we delete the object using operator delete[]
or operator delete
?
The reason I am concerned is if the compiler would be smart enough to convert the statement as a single element allocation int *arr = new int
which would cause calling operator delete[]
UB.
User Case:
I have a pointer, which I would end up allocation in a varied ways but would finally like to get it deleted. So was wondering, for single element allocation, if I consistently use int *arr = new int[1]
can I consistently and safely use operator delete[]
Note
Can you please refer me back to the standards to support your answer?
operator delete[]
of course.int[1]
is notint
. Compiler smart enough won't convert the statement. – johnchen902 yesterdaydelete[]
afternew[]
. there is no special case for size 1. – juanchopanza yesterdayint
just is not the same as anint[1]
. Is a one-element array still an array (hint: answer hidden in this comment)? – Christian Rau yesterdaynew[]
. – James Kanze yesterdaydelete[]
withnew[]
is correct and required by the standard. Such an optimisation by a "smart" compiler would mean the program works on one compiler and not another. That way madness lies. – Jonathan Wakely yesterday