The real difference between Delete and delete []

Source: Internet
Author: User

In C ++, there are two ways to release the new applied memory: delete and delete. What is the difference between the two?

1. We usually see this explanation in the textbook:
Delete releases the memory pointed to by a single object pointer allocated by new.
Delete [] releases the memory pointed to by the new object array pointer.
According to the textbook's understanding, let's look at the following code:
Int * A = new int [10];
Delete A; // method 1
Delete [] A; // method 2
There will certainly be many people saying that method 1 must have a memory leak, right?

(1). For simple types of memory space in array or non-array format after the new method is used, the following two methods can be used:
Int * A = new int [10];
Delete;
Delete [];
In this case, the release effect is the same because the memory size is determined when a simple type of memory is allocated, and the system can remember and manage the memory, the system does not call the destructor,
It can directly obtain the actually allocated memory space through a pointer.
, Even an array of memory space (During the allocation process, the system records the size of the allocated memory and other information., This information is stored in the struct_ CrtmemblockheaderMedium,
For details, see CRT \ SRC \ dbgdel. cpp in the VC installation directory)

(2). For Class, the two methods reflect the specific differences
When you assign an array of class objects in the following ways:
Class
{
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;// Only the memory space pointed to by pointer A is released, but only the 9 Destructor from a [1] to a [9] are called. user-allocated m_cbuffer memory space cannot be releasedThis causes memory leakage.
Delete [] A; // call the destructor of the Class Object to release the memory space allocated by the user and release all the memory space directed by the pointer.
So in summary, if PTR represents a memory space address returned by the memory requested by the new, that is, the so-called pointer, then:
Delete PTR is used to release the memory and only to release the memory directed by PTR.
Delete [] RG is used to release 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 does not have a destructor, delete and delete [] are the same! But if it is a C ++ object array, it will be different!

New [] and delete [] are divided into two types: (1) Allocating and reclaiming space for basic data types; (2) Allocating and reclaiming space for custom data types.

For (1), the program provided above proves that Delete [] is equivalent to delete. But for (2), the situation changes.

Let's take a look at the example below to learn how to use Delete and delete [] in C ++.

# Include <iostream>
Using namespace STD;
//// // Class babe
Class babe
{
Public:
Babe ()
{
Cout <\ "Create a babe to talk with me \" <Endl;
}
~ Babe ()
{
Cout <\ "Babe don \'t go away, listen to me \" <Endl;
}
};
//// // Main Function
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 you only use Delete, when Delete [] is used, three babe don \'t go away and listen to me appear. However, no matter whether the delete or delete [] objects are deleted in the memory, the storage locations are marked as writable, however, when Delete is used, only the destructor of PBabe [0] is called. When Delete [] is used, the destructor of three babe objects are called. You will certainly ask, whatever the case, the storage space is released. What is the difference. A: The key is to call the destructor. This program class does not use the operating system resources (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 objects from the memory, because the system resources will not be released because the destructor of the object is not called, if it is a socket, the socket resource will not be released. The most obvious difference is that the port number is not released, and the maximum Port Number of the system is 65535 (216 _ 1, because there is still 0 ), if the port number is occupied, you cannot access the Internet. If the file resource is not released, you will never be able to modify the file, or even read the file (unless you log off or repeat 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 destructor of these classes. Therefore, when using these classes to generate an array of objects, it is king to use Delete [] to release them. The release with Delete may not cause any problems, and the consequences may be very serious. The specific issue depends on the class code.



The real difference between Delete and delete []

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.