
Question about the operator "delete"
Quote:
> Hello,
>> I have some question about the operator "delete"
>> If I "new" the following array:
>> char * pointer = new char[3][4][5];
>> Then, how should I delete this array?
>> by "delete pointer", or by "delete [] pointer"?
> By iterating through the different instances, ie.
> for (i=0;i<3;i++)
> {
> for(j=0;j<4;j++)
> {
> delete [] pointer[i][j];
> }
> delete [] pointer[i];
> }
> delete [] pointer;
>> Moreover, what is the different between "delete [] pointer" and
>> "delete pointer"?
> "delete [] pointer" indicates to the compiler that a sequence of
> memory segments are to be "deleted" (by reference) while "delete
> pointer" only indicates that the first element should be deleted (ie
> the element on the address that pointer is actually poining at)
More to the point, while both delete and delete[] will deallocate all the
memory regardless of whether it's an array or single object, delete[] is
necessary to have the destructor called on all objects in the array.
Also, you can override both operator delete and operator delete[] seperately
(although I seem to remember reading that VC doesn't implement that
properly, but I don't remember where or what versions of VC this was about).
In that case it's no longer safe to assume that delete and delete[] do the
same thing for an array of destructor-less types.
In short, always use delete[] when dealing with an array.
--
Sven Groot