Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete and delete [] are the same when deleting arrays? [duplicate]

Possible Duplicates:
How could pairing new[] with delete possibly lead to memory leak only?
( POD )freeing memory : is delete[] equal to delete?

Using gcc version 4.1.2 20080704 (Red Hat 4.1.2-48). Haven't tested it on Visual C++.

It seems that delete and delete [] works the same when deleting arrays of "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 deleting arrays of "complex" type, delete will cause 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 are:

  1. In the first test case, why delete and delete [] are the same under g++?
  2. In the second test case, why g++ doesn't handle it like the first test case?
like image 324
jarjar Avatar asked Aug 05 '11 08:08

jarjar


People also ask

What is the difference between delete [] and delete?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer.

What is the effect of delete []?

The operand of delete must be a pointer returned by new , and cannot be a pointer to constant. Deleting a null pointer has no effect. The delete[] operator frees storage allocated for array objects created with new[] . The delete operator frees storage allocated for individual objects created with new .

What happens when you delete an array?

This procedure deletes: All the logical drives on the array. All data on the logical drives that are part of the array.

Can you use Delete on arrays?

Using delete creates empty spots Whatever you do, don't use delete to remove an item from an array. JavaScript language specifies that arrays are sparse, i.e., they can have holes in them. Using delete creates these kinds of holes. It removes an item from the array, but it doesn't update the length property.


5 Answers

This is all dependent on the underlying memory manager. Simply put, C++ requires that you delete arrays with delete[] and delete non-arrays with delete. There is no explanation in the standard for your behaviour.

What's likely happening however is that delete p; simply frees the block of memory starting at p (whether it is an array or not). On the other hand delete[] additionally runs through each element of the array and calls the destructor. Since normal data types like char don't have destructors, there is no effect, so delete and delete[] end up doing the same thing.

Like I said, this is all implementation specific. There's no guarantee that delete will work on arrays of any type. It just happens to work in your case. In C++ we call this undefined behaviour -- it might work, it might not, it might do something totally random and unexpected. You'd be best to avoid relying on undefined behaviour.

like image 120
Peter Alexander Avatar answered Oct 01 '22 02:10

Peter Alexander


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

No. It doesn't gaurantee No memory leak. It in fact invokes undefined behavior.

like image 25
Nawaz Avatar answered Oct 01 '22 04:10

Nawaz


delete and delete[] seemingly being equivalent in g++ is pure luck. Calling delete on memory allocated with new[], and vice versa, is undefined behaviour. Just don't do it.

like image 45
Jörgen Sigvardsson Avatar answered Oct 01 '22 04:10

Jörgen Sigvardsson


Because that's undefined behavior. It's not guaranteed to break but it's not guaranteed to work either.

like image 33
Novikov Avatar answered Oct 01 '22 03:10

Novikov


The delete expression calls the destructor of the object to be deleted before releasing the memory. Releasing the memory probably works in either case (but it's still UB), but if you use delete where you needed delete[], then you aren't calling all the destructors. Since your complex object itself allocates memory which it in turn releases in its own destructor, you are failing to make all those deletions when you use the wrong expression.

like image 37
Kerrek SB Avatar answered Oct 01 '22 03:10

Kerrek SB