Delete VS Delete[] Operators 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++ [duplicate]

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 [].

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.

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.

C/C++ delete vs delete[] [duplicate]

  • 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.

Proper use of delete vs delete[ ] with respect to char * in C++

Your both first code example is wrong.

char * str = new char;  
cin >> str;

You've only allocated memory for a single character. If you read anything other than an empty string, you'll write into unallocated memory and will have undefined behaviour.

if I then delete str, does it delete the array completely?

It will only delete the one character that you allocated. The rest of the string that you wrote in unallocated memory won't be directly affected by the delete. It's not a memory leak, it's a memory corruption.

vs.

char * str = new char[];

This is not legal c++. Array size must be specified.

EDIT: After your fix, the second code is correct as long as you read a string of 29 characters or shorter. If you read a longer string, you'll get undefined behaviour again.

But what if new itself allocates contiguous memory locations?

It doesn't. new (as opposed to new[]) allocates and constructs exactly one object. And delete destroys and deallocates exactly one object.

TLDR Neither program has memory leaks but the first one has undefined behaviour due to memory corruption.

delete vs delete[] [duplicate]

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 !

delete x vs ::operator delete(x)

delete x is a delete expression.

::operator delete(x) is a deallocation function.

The delete expression will call the destructor (if it exists) and then the deallocation function. Calling the deallocation function directly will bypass the destructor.

Delete vs operator delete (and void pointer)

delete ptr will do overload resolution for operator delete, so it may not call the global ::operator delete

But otherwise, yes. The delete operator calls the relevant destructor, if any, and then calls operator delete.

delete[] vs delete in a for loop [duplicate]

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



Submit