Are delete and delete [] the same when deleting arrays?

Possible duplicates:
How can pairing a new [] with deletion only lead to a memory leak?
(POD) free memory: delete [] is equal to delete?

Using gcc version 4.1.2 20080704 (Red Hat 4.1.2-48). Did not test it in Visual C ++.

It seems that delete and delete [] work the same way when deleting arrays of the "simple" type.

 char * a = new char[1024]; delete [] a; // the correct way. no memory leak. char * a = new char[1024]; delete a; // the incorrect way. also NO memory leak. 

But when you delete arrays of a "complex" type, delete will lead to a memory leak.

 class A { public: int m1; int* m2; // a pointer! A() { m2 = new int[1024]; } ~A() { delete [] m2; // destructor won't be called when using delete } }; A* a = new A[1024]; delete [] a; // the correct way. no memory leak. A* a = new A[1024]; delete a; // the incorrect way. MEMORY LEAK!!! 

My questions:

  • In the first test case, why are delete and delete [] same with g ++?
  • In the second test case, why does g ++ not process it like the first test case?
+4
source share
7 answers

It all depends on the underlying memory manager. Simply put, C ++ requires you to delete arrays with delete[] and delete non-arrays with delete . There is no explanation in the standard of your behavior.

However, it is likely that delete p; just frees a block of memory starting at p (regardless of whether the array is or not). On the other hand, delete[] additionally goes through each element of the array and calls the destructor. Since regular data types, such as char , do not have destructors, the effect does not work, so delete and delete[] ultimately do the same.

As I said, all this is implementation specific. There is no guarantee that delete will work on arrays of any type. It just happens in your case. In C ++, we call this behavior undefined - it may work, maybe not, it can do something completely random and unexpected. It’s best not to rely on undefined behavior.

+9
source
 char * a = new char[1024]; delete a; // the incorrect way. also NO memory leak. 

No. It does not support No memory leak . In fact, it causes undefined behavior.

+6
source

delete and delete[] seeming equivalent in g ++ is just luck. The delete call in memory allocated with new[] , and vice versa, is undefined behavior. Just don't do it.

+3
source

Because this behavior is undefined. It is not guaranteed to break, but it also does not guarantee operation.

+1
source

The delete expression invokes the destructor of the object to be deleted before freeing memory. The memory release probably works anyway (but it's still UB), but if you use delete , where you need delete[] , you don't call all destructors. Since your complex object itself allocates memory, which it, in turn, releases in its own destructor, you cannot do all these deletions when using the wrong expression.

+1
source

they technically do not match, they are simply optimized to the same value for non-complex types. complex types require vectorized delete , so a destructor can be called for every object in the you delete array (just like vectorized new for constructors).

what you do just frees memory like its array of pointers.

0
source

What happens here is that when you call delete, the space occupied by the objects is deleted. In the case of characters, this is all you need to do (although it is still recommended to use delete [] because it is just g ++. The actual behavior of the delete call in an undefined array in the C ++ standard).

In the second example, the space occupied by the array is freed, including the m2 pointer. However, what M2 points to is also not deleted. When you call delete [], the destructor on each object in the array is called, and then m2 indicates release.

0
source

All Articles