By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

diff between user defined delete and delete[]

P: n/a
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
Nov 17 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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-lit...html#faq-16.14

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)

Nov 17 '08 #2

P: n/a
On Nov 17, 2:44*pm, Salt_Peter <pj_h...@yahoo.comwrote:
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-lit...html#faq-16.14

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
Nov 17 '08 #3

P: n/a
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:ja*********@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
Nov 17 '08 #4

P: n/a
On Nov 17, 10:44 am, Salt_Peter <pj_h...@yahoo.comwrote:
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-lit...html#faq-16.14
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:ja*********@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

Nov 17 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.