[C ++ foundation] delete and delete [] difference

Finishing Taken: https://www.cnblogs.com/wangjian8888/p/7905176.html

1. difference

1. delete a single object pointer points to release new allocated memory;

1) For simple types, memory size has been determined, destructor direct access memory system may actually allocated and released by the pointer;

2) For an array of class objects, only the first array of calling the destructor of an object, the object space remaining can not be released;

2. delete [] array of objects released new pointer to the allocated memory.

1) For simple types, the effect is the same delete, can release an array of memory space;

2) For an array of class objects, one by one array call the destructor for each object, the release of all the memory space pointer.

2. For a simple type

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

In this case the same release effect due to: assign House simple type, the memory size has been determined, the memory system can be managed and, on destruction, the system does not call the destructor directly, it gets directly hands actually allocated memory space (memory array will also be recorded system, stored in the structure CrtMemBlockHeader).

3. For class array of objects

 1 class A
 2 {
 3 private:
 4     char *m_cBuffer;
 5     int m_nLen;
 6 public:
 7     A() {m_cBuffer = new char[m_nLen]; }
 8     ~A() {delete [] m_cBuffer;}          
 9 };
10 
11 A *a = new A[10];
12 delete a;  // (1)
13 delete []a; //(2)

For (1), only the release of all of the memory space a pointer, i.e. destructor calls a [0] of the object;

But the remaining a [1], ..., a [9] nine objects of discretionary m_cBuffer corresponding memory space will not be released.

g ++ 11 compiler, run error: *** Error in `./delete_test ': double free or corruption (fasttop): 0x0000000000ef4c20 ***

For (2), calls the destructor of an object class object itself release the memory space allocated, the array can be individually call the destructor for each object, all of the memory space a release pointer.

4. Examples Description

 1 #include <iostream>
 2 using namespace std;
 3 /////////class Babe
 4 class Babe
 5 {
 6 public:
 7     Babe()
 8     {
 9         cout << "Create a Babe to talk with me" << endl;
10     }
11     ~Babe()
12     {
13         cout << "Babe don\'t go away,listen to me" << endl;
14     }
15 };
16 //////////main function
17 int main()
18 {
19     /* (1) */
20     // Babe* pbabe1 = new Babe[3];
21     // delete pbabe1;
22 
23     /* (2) */
24     Babe* pbabe2 = new Babe[3];
25     delete [] pbabe2;
26     return 0;
27 }
28 
29 /* (1) Output: */
30 // Create a Babe to talk with me
31 // Create a Babe to talk with me
32 // Create a Babe to talk with me
33 // Babe don't go away,listen to me
34 // *** Error in `./delete_test': munmap_chunk(): invalid pointer: 0x00000000018a6c28 ***
35 
36 /* (2) Output: */
37 // Create a Babe to talk with me
38 // Create a Babe to talk with me
39 // Create a Babe to talk with me
40 // Babe don't go away,listen to me
41 // Babe don't go away,listen to me
42 // Babe don't go away,listen to me

The original blog to display different results, with g ++ 11 compiler, run-time (1) error.

When the array free space, suggested the use delete [], if the type of program use system resources of the operating system (Socket, File, Thread, etc.), using the delete [] will make the objects one by calling the destructor, freeing system resources.

Guess you like

Origin www.cnblogs.com/shiyublog/p/10986278.html