简体   繁体   中英

delete vs delete[] operators in C++

What is the difference between delete and 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 delete[]<\/code> operator is used to delete arrays. The delete<\/code> operator is used to delete non-array objects. It calls operator delete[]<\/code> and operator delete<\/code> function respectively to delete the memory that the array or non-array object occupied after (eventually) calling the destructors for the array's elements or the non-array object.

typedef int array_type[1];

// create and destroy a int[1]
array_type *a = new array_type;
delete [] a;

// create and destroy an int
int *b = new int;
delete b;

// create and destroy an int[1]
int *c = new int[1];
delete[] c;

// create and destroy an int[1][2]
int (*d)[2] = new int[1][2];
delete [] d;

This the basic usage of allocate\/DE-allocate pattern in c++ malloc<\/code> \/ free<\/code> , new<\/code> \/ delete<\/code> , new[]<\/code> \/ delete[]<\/code>

But I would like to add this particular understanding for the difference between delete<\/code> and delete[]<\/code>

class ABC{}

ABC *ptr = new ABC[100]

The operators delete and delete [] are used respectively to destroy the objects created with new and new[] , returning to the allocated memory left available to the compiler's memory manager.

Objects created with new must necessarily be destroyed with delete , and that the arrays created with new[] should be deleted with delete[] .

When I asked this question, my real question was, "is there a difference between the two? Doesn't the runtime have to keep information about the array size, and so will it not be able to tell which one we mean?" This question does not appear in "related questions", so just to help out those like me, here is the answer to that: "why do we even need the delete[] operator?"<\/a>

"

C++ delete[] operator ensures that Destructor for all object allocated with new[] is called. The following example demonstrates the same. Also, delete[] must be preferred (if new[] used previously) when the class has a non-default destructor to release the acquired resources. Otherwise, it might result in memory leaks.

Common Code:-

#include <iostream>
using namespace std;

class memTest{
    public:
    static int num;
    memTest(){
cout<<"Constructor from object " << num++ << endl;
    }
    ~memTest(){
cout<<"Destructor from object " << --num << endl;        
    }
};
int memTest::num=0;

Example 1:- use of new[] and delete may result in undefined behavior.

int main() {
    memTest* Test1=new memTest[3];
    
    delete Test1; //<-----
    return 0;
} 

Output 1:-

Constructor from object 0
Constructor from object 1
Constructor from object 2
Destructor from object 2 //<-----

Example 2: The correct behavior is using new[] and delete[].

int main() {
    memTest* Test1=new memTest[3];
    
    delete[] Test1; //<-----
    return 0;
} 

Output 2:-

Constructor from object 0
Constructor from object 1
Constructor from object 2
Destructor from object 2
Destructor from object 1 //<-----
Destructor from object 0 //<-----

delete<\/code> is used for one single pointer and delete[]<\/code> is used for deleting an array through a pointer. This<\/a> might help you to understand better.

"

Well.. maybe i think it is just for explicit. The operator delete and operator delete[] is distinguished but delete operator can release array too.

test* a = new test[100];
delete a;

And already checked this code have no memeory leak.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM