Can I use `operator delete []` to select one element of an array?

If we select an object size 1 below

int *arr = new int[1]; 

Should I delete an object using operator delete[] or operator delete ?

The reason I'm interested is because the compiler would be smart enough to convert the operator as a selection of a single element int *arr = new int , which would invoke the operator delete[] UB call.

User case:

I have a pointer that I would distribute in various ways, but finally would like to delete it. So it was interesting to highlight individual elements, if I use int *arr = new int[1] sequentially, can I use operator delete[] sequentially and safely

Note

Could you return me to standards to support your answer?

+7
c ++
source share
6 answers

You should use delete[] , not delete . The compiler is not allowed to change new int[1] to new int .

(As int is a POD type, it is entirely possible that new int and new int[1] do exactly the same thing under covers, but if so, delete[] on and int* and delete on int* will also do the same the most.)

ISO / IEC 14882: 2011 5.3.5 [expr.delete] / 2:

In the first alternative (to delete an object), the value of the delete operand can be a null pointer value, a pointer to an object without an array created by the previous new expression, or a pointer to a subobject (1.8) that represents the base class of such an object (Section 10). If not, the behavior is undefined.

Since int[1] is an array object, if you try to delete it with delete rather than delete[] , it will be undefined.

+13
source share

The rule is pretty simple:

  • always balance a new with delete
  • always balance a new[] with delete[]

otherwise, you will get undefined behavior.

There are no exceptions; even new[1] must be balanced with delete[] , and new[0] must be deleted, since the compiler can still reserve storage.

+6
source share

Should I delete an object using the delete[] operator delete or operator delete ?

operator delete[] . You have allocated an array from int s, so there is no other way to free it properly. Using operator delete will cause undefined behavior.

+2
source share

You must use delete[] .

C ++ 11 5.3.5 Delete

:: opt delete cast-expression

:: opt delete [] cast-expression

The first option is for objects without an array, and the second is for arrays.

An array containing one element is still an array.

+2
source share
 int *arr = new int[1]; 

Since you use [] for highlighting, you need to use [] for delete .

Memory leak

Objects created by new expressions (objects with dynamic storage duration) are stored until the pointer returned by the new expression is used in the mapping .

So you must use delete[] to free memory, otherwise you will get undefined behavior.

And (incase) , if complier is smart enough to accept int * arr = new int[1] as int , it should be smart enough to take delete[] arr on arr as delete arr . However, there is nothing for delete to distinguish arr from int*arr = new int[1] and int*arr = new int . [] in delete will indicate it.

+1
source share

No, this is not safe, it can lead to some undesirable situations.
For example, some data ( int/char/float ...) may be in memory near the place where your pointer points. At that time, if you used delete [] , then it may try to delete this data as well, if it is an integer, and for other data types it leads to unexpected errors.

Example:

 int *ptr = new int; int a = 20; char c = 'e'; *ptr = 10; delete [] ptr; 

There is a possibility that the variables c and a may be stored near the place where the ptr pointer points. If a is stored next to it, then it deletes " a " also, if other data types are present, leads to unexpected results.

Therefore, it is recommended that you use delete[] only to delete the allocated memory using the new data type [] . Arranging this is useful.

0
source share

All Articles