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:
delete
and delete []
are the same under g++?delete is used for one single pointer and delete[] is used for deleting an array through a pointer.
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 .
This procedure deletes: All the logical drives on the array. All data on the logical drives that are part of the array.
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.
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.
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.
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.
Because that's undefined behavior. It's not guaranteed to break but it's not guaranteed to work either.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With