[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

diff between user defined delete and delete[]

mail.dsp

11/17/2008 9:13:00 AM

Suppose in a class we overload four operators:
operator new
operator delete
operator new[]
operator delete[]

class Test{
public:
void * operator new (size_t t){
cout<<"\nCalling... new";
return malloc(t);
}
void operator delete (void *p){
cout<<"\nCalling ... delete";
free(p);
}
void * operator new [] (size_t t){
cout<<"\nCalling ... new[]";
return malloc(t);
}

void operator delete [] (void *p){
cout<<"\nCalling ... delete[]";
free(p);
}
};

If we perform
Test *p=0; delete p;
It calls operator delete.

But if we perform
Test *p=0; delete []p;
It doesn't call operator delete[] until and unless we don't call
operator new[]. Means If we do like this;
Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].

Why???

I've executed this code on g++ 4.1.2 20070925.

Thanks in advance
--
Daya
4 Answers

Salt_Peter

11/17/2008 9:44:00 AM

0

On Nov 17, 4:13 am, mail....@gmail.com wrote:
> Suppose in a class we overload four operators:
> operator new
> operator delete
> operator new[]
> operator delete[]
>
> class Test{
> public:
> void * operator new (size_t t){
> cout<<"\nCalling... new";
> return malloc(t);
> }
> void operator delete (void *p){
> cout<<"\nCalling ... delete";
> free(p);
> }
> void * operator new [] (size_t t){
> cout<<"\nCalling ... new[]";
> return malloc(t);
> }
>
> void operator delete [] (void *p){
> cout<<"\nCalling ... delete[]";
> free(p);
> }
>
> };
>
> If we perform
> Test *p=0; delete p;
> It calls operator delete.
>
> But if we perform
> Test *p=0; delete []p;
> It doesn't call operator delete[] until and unless we don't call
> operator new[]. Means If we do like this;
> Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].
>
> Why???
>

The FAQ covers the subject. Read the 2 common techniques used with
primitive arrays

[16.14] After p = new Fred[n], how does the compiler know there are n
objects to be destructed during delete[] p?
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html...

Although in modern code unless there is a really good reason:
a) don't allocate on the heap manually, use smart pointers or RAII
b) prefer dynamic containers over primitive, fixed arrays
(std::vector, std::deque, etc)

Sachin

11/17/2008 10:52:00 AM

0

On Nov 17, 2:44 pm, Salt_Peter <pj_h...@yahoo.com> wrote:
> On Nov 17, 4:13 am, mail....@gmail.com wrote:
>
>
>
>
>
> > Suppose in a class we overload four operators:
> > operator new
> > operator delete
> > operator new[]
> > operator delete[]
>
> > class Test{
> > public:
> >         void * operator new (size_t t){
> >                 cout<<"\nCalling... new";
> >                 return malloc(t);
> >         }
> >         void operator delete (void *p){
> >                 cout<<"\nCalling ... delete";
> >                 free(p);
> >         }
> >         void * operator new [] (size_t t){
> >                 cout<<"\nCalling ... new[]";
> >                 return malloc(t);
> >         }
>
> >         void operator delete [] (void *p){
> >                 cout<<"\nCalling ... delete[]";
> >                 free(p);
> >         }
>
> > };
>
> > If we perform
> > Test *p=0;     delete p;
> > It calls operator delete.
>
> > But if we perform
> > Test *p=0;  delete []p;
> > It doesn't call operator delete[] until and unless we don't call
> > operator new[]. Means If we do like this;
> > Test *p=0;   p=new Test[10];   delete[] p; It calls operator delete[].
>
> > Why???
>
> The FAQ covers the subject. Read the 2 common techniques used with
> primitive arrays
>
> [16.14] After p = new Fred[n], how does the compiler know there are n
> objects to be destructed during delete[] p?http://www.parashift.com/c++-faq-lite/freestore-mgmt.html...
>
> Although in modern code unless there is a really good reason:
> a) don't allocate on the heap manually, use smart pointers or RAII
> b) prefer dynamic containers over primitive, fixed arrays
> (std::vector, std::deque, etc)- Hide quoted text -
>
> - Show quoted text -

VC++ Compiler does call delete[], even if you dont call new[]
explicitly.

Thx,
Sachin

James Kanze

11/17/2008 1:31:00 PM

0

On Nov 17, 10:13 am, mail....@gmail.com wrote:
> Suppose in a class we overload four operators:
> operator new
> operator delete
> operator new[]
> operator delete[]

> class Test{
> public:
> void * operator new (size_t t){
> cout<<"\nCalling... new";
> return malloc(t);
> }
> void operator delete (void *p){
> cout<<"\nCalling ... delete";
> free(p);
> }
> void * operator new [] (size_t t){
> cout<<"\nCalling ... new[]";
> return malloc(t);
> }
>
> void operator delete [] (void *p){
> cout<<"\nCalling ... delete[]";
> free(p);
> }
> };

> If we perform
> Test *p=0; delete p;
> It calls operator delete.

> But if we perform
> Test *p=0; delete []p;
> It doesn't call operator delete[] until and unless we don't
> call operator new[].

I'm not sure how to read the above. Too many negations. But in
a delete expression, if the pointer is null, it is unspecified
whether the compiler calls the operator delete function or not.
Probably, your compiler calls it in the simple cases, but
doesn't bother calling it if it otherwise has to check for a
null pointer (usually the case with delete[]).

> Means If we do like this;
> Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].

> Why???

Why what? If the pointer isn't null, the compiler is required
to call operator delete. If the pointer is null, it is
unspecified whether operator delete is called or not (so the
operator delete function had better check). And it can
sometimes do one, sometimes the other---adding a virtual
destructor (or even any non-trivial destructor) might change
the behavior, for example.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

James Kanze

11/17/2008 1:39:00 PM

0

On Nov 17, 10:44 am, Salt_Peter <pj_h...@yahoo.com> wrote:
> On Nov 17, 4:13 am, mail....@gmail.com wrote:

[...]
> > If we perform
> > Test *p=0; delete p;
> > It calls operator delete.

> > But if we perform
> > Test *p=0; delete []p;
> > It doesn't call operator delete[] until and unless we don't call
> > operator new[]. Means If we do like this;
> > Test *p=0; p=new Test[10]; delete[] p; It calls operator delete[].

> > Why???

> The FAQ covers the subject. Read the 2 common techniques used
> with primitive arrays

I don't think the FAQ has anything which addresses his question;
I couldn't find anything, anyway.

> [16.14] After p = new Fred[n], how does the compiler know there are n
> objects to be destructed during delete[] p?http://www.parashift.com/c++-faq-lite/freestore-mgmt.html...

Which doesn't say anything about what happens when you pass a
null pointer to delete[] (and why what happens is, or may be,
different than what happens when you pass a null pointer to a
non-array delete). (Given that this is the first time I've seen
this question asked, it's not too surprising that it isn't in
the *Frequently* Asked Questions.

> Although in modern code unless there is a really good reason:
> a) don't allocate on the heap manually, use smart pointers or RAII

I'm not following you here. None of the smart pointers I've
seen take care of allocation; despite the name, nor does the
RAII idiom.

> b) prefer dynamic containers over primitive, fixed arrays
> (std::vector, std::deque, etc)

Agreed. And more generally, don't use dynamic allocation
(except that hidden within such containers) unless you need it
(in which case, smart pointers and RAII probably won't help).

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34