Delete is really different from delete []

Source: Internet
Author: User

we usually see this description from the textbook:
Delete releases the memory that is pointed to by the single object pointer assigned by new
Delete[] Releases the memory that is pointed to by the object array pointer of new assignment
So, according to the textbook understanding, let's look at the following code:
int *a = new INT[10];
Delete A; Mode 1
delete [] A; Mode 2
There's going to be a lot of people saying that mode 1 definitely has a memory leak, is that it?

1. Using the new assignment for a simple type either an array or a non-array form of memory space is available in either of two ways :
int *a = new INT[10];
Delete A;
delete [] A;
The release effect is the same in this case: when allocating a simple type of memory, the memory size has been determined, the system can be remembered and managed, at the time of destruction, the system does not call the destructor, it directly through the pointer can get the actual allocated memory space, even an array of memory space (in the allocation process Information such as the size of the allocated memory is recorded, and this information is stored in the structure _crtmemblockheader.
For details, see VC installation directory under Crt\src\dbgdel.cpp)

2. For class classes, two ways to reflect the specific differences
When you assign 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 the full memory space that the A pointer points to is freed but only the destructor of the A[0] object is left from a[1] to a[9] The 9 user-allocated m_cbuffer corresponding memory space will not be freed and thus cause a memory leak.
delete [] A; Call a destructor that uses a class object to release the user's own allocated memory space and free all memory space pointed to by a pointer
So, in summary, if PTR represents a memory space address, known as a pointer, that is returned with the memory requested by new, then:
Delete ptr is used to free memory and is used only to free the memory that PTR points to.
Delete[] RG is used to release RG pointing to the memory,!! The destructor!! of each object in the array is also called individually
for simple data types like int/char/long/int*/struct and so on, because the object is not destructor, delete and delete [] are the same! But if it's a C + + object array, it's different!

About new[] and delete[], which are divided into two situations: (1) Allocate and reclaim space for basic data types, and (2) allocate and reclaim space for custom types.

for (1), the program provided above has shown that delete[] and delete are equivalent. But for (2), the situation has changed.



Delete is really different from 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.