Question about the operator "delete" 
Author Message
 Question about the operator "delete"

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"?

Moreover, what is the different between "delete [] pointer" and "delete
pointer"?

THANKS



Wed, 10 Aug 2005 00:04:36 GMT  
 Question about the operator "delete"
Hello,

Quote:

> 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];

Quote:
}

delete [] pointer;

Quote:

> 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)

Sincerely
tlwc



Wed, 10 Aug 2005 00:40:05 GMT  
 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



Wed, 10 Aug 2005 03:05:07 GMT  
 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;

Nonsense. There was just one call to new - why do you think you need
multiple calls to delete[] ?
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Wed, 10 Aug 2005 05:26:32 GMT  
 Question about the operator "delete"

Quote:
> I have some question about the operator "delete"

> If I "new" the following array:

> char * pointer = new char[3][4][5];

It won't compile. The type of (new char[3][4][5]) expression is
char(*)[4][5], not char*
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken



Wed, 10 Aug 2005 05:30:36 GMT  
 Question about the operator "delete"
Hello again Igor,

Quote:

> Nonsense. There was just one call to new - why do you think you need
> multiple calls to delete[] ?

Well I ofcourse assumed that he shortened the question a bit. You need
to declare a char*** and create each array. Something like the example
below. The resulting 3-dim array will be a testarray[3][4][10].

#include <iostream.h>
#include <string.h>
#include <stdio.h>

int main()
{
        int i=0,j=0;
        char ***testarray=NULL;
        testarray=new char**[3];
        for (i=0;i<3;i++)
        {
                testarray[i]=new char*[4];
                for (j=0;j<4;j++)
                {
                        testarray[i][j]=new char[10];
                        strcpy(testarray[i][j],"hejsan");
                }
        }
        for (i=0;i<3;i++)
        {
                for (j=0;j<4;j++)
                {      
                        cout<<i<<":"<<j<<"="<<testarray[i][j]<<endl;
                        delete [] testarray[i][j];
                }
                delete [] testarray[i];
        }
        delete [] testarray;

        return 0;

Quote:
}

Sincerely
tlwc


Fri, 12 Aug 2005 19:22:04 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. C++ question with "delete"

2. error LNK2005: "public: static void __stdcall CObject::operator delete help

3. error LNK2005: "public: static void __stdcall CObject::operator delete help

4. "*" Operator Result Type

5. operator ","??

6. Usage of "==" operator for doubles / floats

7. "integral promotions" and unary arithmetic operators

8. "modulas" operator in C

9. Stack Overflow when calling "new" operator

10. Usage of "==" operator for doubles / floats

11. Linking a "Template Class Friend Operator"

12. Overloading operator "="

 

 
Powered by phpBB® Forum Software