The difference between delete and delete[in C + + _c language

Source: Internet
Author: User

has been on C + + DELETE and delete[] The difference is not very understanding, today encountered, online check, came to the conclusion. Make a backup to avoid loss.

C + + tells us to use delete[when retrieving the memory space of a single object allocated with new, using Delete to reclaim the memory space of a group of objects allocated with new[]. About new[] and delete[], which are divided into two situations: (1) allocating and reclaiming space for basic data types, (2) allocating and reclaiming space for custom types.

Please see the following procedure.

#include <iostream>;
using namespace std;
 
Class T {public
:
 t () {cout << "constructor" << Endl;}
 ~t () {cout << "destructor" << Endl;}
};
 
int main ()
{
 const int NUM = 3;
 
 t* p1 = new T[num];
 cout << hex << p1 << Endl;
 Delete[] P1;
 Delete P1;
 
 t* P2 = new T[num];
 cout << p2 << endl;
 Delete[] P2
}
 

You can run this program yourself, look at the different results of the delete P1 and delete[] P1, I don't post the results here.

From the results of the operation we can see that the delete P1 in the process of reclaiming space, only p1[0] This object calls the destructor, other objects such as p1[1], p1[2] and so did not invoke their own destructor, which is the crux of the problem.      If you use delete[], all objects will first call their destructor before reclaiming space. Objects of the base type do not have destructors, so it is possible to reclaim the array space of the base type with delete and delete[], but for the class object array, use only delete[].      For a single object of new, you can only use Delete to reclaim space with delete[. So a simple use principle is: New and delete, new[] and delete[] correspond to use.

My understanding, when you use Delete to release the memory space that is requested with the new int[], because it has no destructor for the base data type, the same is used with delete and delete [], both of which release the requested memory space, if the custom data type, when there is a destructor, with new [ The requested space must be released with delete []. Because the destructor of the object array is called one at a time to delete [], and then the space is freed, and if you use Delete, only the destructor of the first object is called, and the destructor of the object is not invoked, so is the space released??

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.