The Difference Between Delete and Delete[] in C++

delete vs delete[] operators in C++

The delete operator deallocates memory and calls the destructor for a single object created with new.

The delete [] operator deallocates memory and calls destructors for an array of objects created with new [].

Using delete on a pointer returned by new [] or delete [] on a pointer returned by new results in undefined behavior.

The difference between delete and delete[] in C++

You delete [] when you newed an array type, and delete when you didn't. Examples:

typedef int int_array[10];

int* a = new int;
int* b = new int[10];
int* c = new int_array;

delete a;
delete[] b;
delete[] c; // this is a must! even if the new-line didn't use [].

Difference between delete() function and delete operator in c++?

delete() function also do the same job in below code

delete (ptr1); 

That is not a "delete function". That is a delete expression where the "operand" is a parenthesised expression.

what is difference [between delete ptr; and delete (ptr1);]

The difference is the same as in the following expressions:

1 +  2
1 + (2)

In other words, there is no functional difference. The parentheses are redundant.

What's the difference between delete[] and ::operator delete() in c++

The new-expressions new T and new T[n] create objects in memory that they acquire.

The delete-expressions delete p and delete [] p destroy objects and release the memory where they were stored.

operator new is a memory allocation function, and operator delete is a memory deallocation function. They do not do anything more than manage memory, and correspond to C's malloc and free.
They have those names in order to avoid introducing more keywords to the language – "operator new" and "operator delete" are just funky ways of spelling "allocate" and "deallocate".

The :: is the scope resolution operator and makes sure that these calls are specifically to the functions defined in the global scope.

The new-expressions and delete-expressions are not equivalent to these functions, but use them behind the scenes for memory management.

If you use operator new for allocating memory you must then also create an object in that memory, and if you release memory with operator delete you should first destroy the object that occupies the memory.

delete vs delete[]

From the standard (5.3.5/2) :

In the first alternative (delete
object), the value of the operand of
delete shall be a pointer to a
non-array object or a pointer to a
sub-object (1.8) representing a base
class of such an object (clause 10).
If not, the behavior is undefined.

In the second alternative (delete
array), the value of the operand of
delete shall be the pointer value
which resulted from a previous array
new-expression. If not, the
behavior is undefined.

So no : they are in no way equivalent !

C/C++ delete vs delete[]

  • delete: This frees the memory currently allocated by the pointer the delete is performed upon. It only deletes the memory pointed to by the first variable.

  • delete []: This frees the memory allocated for the whole array. An array consists of several variables - delete frees memory only allocated for the first variable, while delete [] does the whole thing.

A good way to think of it is considering delete as an instruction while delete [] as a loop; where the array is looped through and delete is called individually on each variable in the array. This is NOT how it works in reality (the real workings are a bit more complicated), but is a good way to understand the diff.

The destructor is called on all objects, because in some cases such as in the case of an array of objects that contain pointers, calling the destructor on only the first element doesn't free all memory.

What's the difference between delete[] arr and deleting elements in a loop

Well, option 1 exhibits undefined behavior, so the difference is one of correct code and incorrect code.

You can only delete (or, in this case, delete[]) what was returned from new. The individual elements of the array were not returned by new (nor are they pointers). Calling delete on a pointer which was not returned by new, i.e., array + n invokes undefined behavior.

An implementation of new may allocate a bit more memory than was requested for bookkeeping. It gets a pointer, p, and then says "ok, now let's look at the info new created for me at p - sizeof(some_structure). Now I know that I allocated n bytes of memory because some_structure.n tells me so, so I'll clean that up now".

When it attempts to do that on the erroneous pointer you gave it it reads nonsense and anything can happen.

On a side note, prefer std::copy to copy an array, not a loop.

delete[] vs delete in a for loop

Both versions of new and delete each have two tasks: allocation/deallocation and construction/destruction.

  • new will allocate memory and call a constructor.

  • delete will call a deconstructor and deallocate memory.

  • new [] allocates single chunk of memory and then calls a constructor possibly several times.

  • delete [] calls a deconstructor possibly several times and then deallocates a single chunk of memory.

So using delete multiple times means deallocating multiple chunks of memory whereas using delete[] will deallocate a single chunk of memory; using delete multiple times is not equivalent to using delete [].

Related Topics

Leave a reply