Difference between C++ delete and delete []

Posted by landung on Tue, 11 Jan 2022 01:35:15 +0100

delete frees the memory pointed to by the single object pointer allocated by new
delete [] releases the memory pointed to by the pointer of the object array allocated by new
Then, according to the understanding of the textbook, let's look at the following code:

int *a = new int[10];
delete a;        //Mode 1
delete [] a;     //Mode 2

There must be a lot of people saying that there must be a memory leak in mode 1, is that right?

(1). The memory space allocated by new for simple types, whether in array or non array form, can be used in two ways, such as:

int *a = new int[10];
delete a;
delete [] a;


In this case, the release effect is the same because when allocating simple type memory, the memory size has been determined, and the system can remember and manage it. During destructor, the system will not call the destructor,
It can obtain the actual allocated memory space, even an array memory space, directly through the pointer (during the allocation process, the system will record the size of the allocated memory and other information, which is stored in the structure _CrtMemBlockHeader,

(2). For Class, the two methods reflect specific differences
When you allocate an array of class objects in the following ways:

class A
{
private:
    char *m_cBuffer;
    int m_nLen;
public:
    A()
    {
        m_cBuffer = new char[m_nLen];
    }
    ~A()
    {
        delete [] m_cBuffer;
    }
};
A *a = new A[10];
delete a; 
/*Only all memory space pointed to by the a pointer is released, but only the destructor of the a[0] object is called 
The remaining m allocated by 9 users from a[1] to a[9]_ The memory space corresponding to CBuffer cannot be released 
This causes a memory leak*/
delete [] a;     
/*Call the destructor using the class object to free the memory space allocated by the user, and   
Frees up all memory space pointed to by the a pointer
*/


So to sum up, if ptr represents a memory space address returned from the memory applied for with new, that is, the so-called pointer, then:
delete , ptr , is used to free memory, and is only used to free the memory pointed to by ptr.
delete [] rg is used to free the memory pointed to by rg,!! Also call the destructor of each object in the array one by one!!
For simple data types such as int/char/long/int*/struct, because the object has no destructor, delete and delete [] are the same! But if it's a C + + object array, it's different!

About new [] and delete [], there are two cases: (1) allocate and recycle space for basic data types; (2) Allocate and recycle space for custom types.
For (1), the program provided above has proved that delete [] and delete are equivalent. But for (2), the situation changes.
Let's take a look at the following examples to learn how to use delete and delete [] in C + +
 

#include <iostream>
using namespace std;
class Babe
{
public:
    Babe()
    {
        cout << "Create a Babe to talk with me" << endl;
    }
    ~Babe()
    {
        cout << "Babe don't go away,listen to me" << endl;
    }
};
int main()
{
    Babe* pbabe = new Babe[3];
    delete pbabe;
    pbabe = new Babe[3];
    delete []pbabe;
    return 0;
}


The result is:

Create a Babe to talk with me
Create a Babe to talk with me
Create a Babe to talk with me
Babe don't go away,listen to me
Create a Babe to talk with me
Create a Babe to talk with me
Create a Babe to talk with me
Babe don't go away,listen to me
Babe don't go away,listen to me
Babe don't go away,listen to me


As you can see, only one Babe don't go away,listen to me appears when using delete [] only, while three Babe don't go away,listen to me appear when using delete []. However, no matter whether delete or delete [] is used, the three objects are deleted in memory. Both the storage locations are marked as writable, but only the destructor of pbabe[0] is called when delete is used, while the destructor of three Babe objects is called when delete [] is used.

You must ask, what's the difference between releasing the storage space anyway. A: the key is to call the destructor. The class of this program does not use the system resources of the operating system (such as Socket, File, Thread, etc.), so it will not cause obvious consequences. If your class uses operating system resources, it is inappropriate to simply delete the class object from memory, because if you do not call the destructor of the object, the system resources will not be released. If it is a Socket, the Socket resources will not be released. The most obvious is that the port number will not be released. The maximum port number of the system is 65535 (216 1, because there is 0), If the port number is occupied, you can't surf the Internet, ha ha. If the File resource is not released, you can never modify the File or even read the File (unless you log off or restart the system). If the Thread is not released, it will always run in the background, wasting memory and CPU resources. The release of these resources must rely on the destructors of these classes. Therefore, when using these classes to generate object arrays, using delete [] to release them is the king. The use of delete to release may not cause problems, or the consequences may be serious, depending on the class code

Topics: C++