C++:delete and delete[] The difference between releasing memory

Source: Internet
Author: User

C + + tells us to use delete[] when reclaiming the memory space of a single object allocated with new and using Delete to reclaim the memory space of a set of objects allocated with new[]. 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. Please see the procedure below. Copy the code code as follows: #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 on your own, and look at the different results of the delete P1 and delete[] P1, and I won't post the results here. from the running results we can see that delete P1 in the process of reclaiming space, only p1[0] This object called the destructor, other objects such as p1[1], p1[2] and so on have not called their own destructor, this is the crux of the problem.      If you use delete[], all objects will first call their own destructors before reclaiming the space. Basic types of objects do not have destructors, so it should be possible to reclaim the array space of basic types with delete and delete[], but only with delete[] for an array of class objects.      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[] corresponding to use.  I understand that when you use Delete to free the memory space requested by new int[], because it has no destructor for the base data type, using delete is the same as delete [], both of which will free up the requested memory space, if the custom data type has a destructor, The space requested by new [] must be freed with delete [], because the destructor of the object array is called one at a time to delete [], then the space is freed, and if you use Delete, only the destructor of the first object is called, and the destructor for the object is not called. So is the space released??    Delete Releases the memory that is pointed to by the single object pointer assigned by newdelete[] Releases the memory that is pointed to by the object array pointer of new assignmentSo, according to the textbook understanding, let's look at the following code:int *a = new INT[10];Delete A; Mode 1Delete [] A; Mode 2There'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 in this case is the same because when allocating a simple type of memory, the memory size has been determined, the system can be remembered and managed, and at the time of destruction, the system does not call the destructor.it can get the actual allocated memory space directly through the pointer, even if it is an array memory space (the system records the size of the allocated memory during the allocation process, 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 differencesWhen 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 a[0] object's destructor is left from a[1] to a[9] The 9 user-assigned m_cbuffer corresponding memory space will not be freedthus causing a memory leakDelete [] A; Frees all memory space pointed to by the A pointer and calls the destructor of the class object to release the user's own allocated memory space   The use of Delete or delete[] for arrays of simple data type Int,char in VCs is exactly the same, but for an array of instances of a class you must use delete[], otherwise the cleavage function of the class may not be called correctly.  The above quoted: http://www.cnblogs.com/zhengyuhong/articles/delete1.htmlSummary: It is best to use delete[] p to remove pointers when it is not clear whether the pointer is an array or a single variable. does not cause the above-mentioned indirect memory leaks.

C++:delete and delete[] The difference between releasing memory

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.