473,411 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

diff between user defined delete and delete[]

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
4 1822
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
9
by: YZK | last post by:
Hello. I'm not a Web developer, just a user, and I think I may have somehow messed myself up majorly. I'm not quite sure how. Right now, javascript used by websites I go to either does not work at...
0
by: keith | last post by:
I have an Window app that load a user control usercontrol1.dll b Assembly assembly=Assembly.LoadFrom("path\usercontrol1.dll") Control ui=assembly.CreateInstance("NameSpace.ClassName) as Control ...
8
by: Razak | last post by:
Hi, I have a class which basically do Impersonation in my web application. From MS KB sample:- ++++++++++++++++++++code starts Dim impersonationContext As...
1
by: Mphoza | last post by:
Hi! Programmers,,,,,, Scenario I have a DataGrid with the delete button, If the user delete the row I'd like to show a confirmation dialog box with two buttons(Yes,No). If the user click yes...
4
by: Andreas Kasparek | last post by:
Hola! I'm preparing my master thesis about a XML Merge Tool implementation and was wondering if there is any open standard for XML diff regarding topics like: - is a diff result computed on...
17
by: Rob Hoelz | last post by:
I've been compiling a list of the differences between C and C++, and I'd like to see how thorough I've been. Could any of you go over this list and see if I've missed anything? Here's the list:...
10
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a...
6
by: Aaron Gray | last post by:
Hi, I am working on an HTML WYSISYG Wiki and need to display a diff page like WikiPedia does if two people edit a file at the same time to give the second user the diff. Basically with additions...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.