C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

for every overloaded new we need to declate a delete with si

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Rajaram S. Gaunker
Guest





PostPosted: Wed Dec 17, 2003 10:13 am    Post subject: for every overloaded new we need to declate a delete with si Reply with quote



hi all

for every overloaded "operator new" we need to overload "operator
delete" with similar signature.

I've read in D&E that this is not the case but in Scott Mayers book
"Effective c++" i read that u need to do that.
Is it a new change in standrads.

Please clarify.
rajaram

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Wed Dec 17, 2003 4:58 pm    Post subject: Re: for every overloaded new we need to declate a delete wit Reply with quote



In article <e3c5fdc7.0312162042.75abff25 (AT) posting (DOT) google.com>, Rajaram S.
Gaunker <rajaram (AT) emuzed (DOT) com> writes
Quote:
hi all

for every overloaded "operator new" we need to overload "operator
delete" with similar signature.

I've read in D&E that this is not the case but in Scott Mayers book
"Effective c++" i read that u need to do that.
Is it a new change in standrads.

Please clarify.

The issue is more complicated. Whenever we write an overload for
operator new we must consider whether we should also write an operator
delete. If we are writing a global (often called placement) overload for
operator new we have no choice, because there is no mechanism for
overloading operator delete at that level (hence the advice in D&E). But
when we write a class scope operator new we would normally also write a
corresponding class scope operator delete. If we do not, the global one
will be used instead, and that would often do the wrong thing (hence the
advice given in EC++)


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Wed Dec 17, 2003 7:13 pm    Post subject: Re: for every overloaded new we need to declate a delete wit Reply with quote



On Wed, 17 Dec 2003 05:13:50 -0500, Rajaram S. Gaunker wrote:

Quote:
hi all

for every overloaded "operator new" we need to overload "operator
delete" with similar signature.

I've read in D&E that this is not the case but in Scott Mayers book
"Effective c++" i read that u need to do that.
Is it a new change in standrads.

It is not that we NEED to strictly define operator delete for operator new
defined by us, but consider the case where you operator new does something
else apart from memory allocation, or perhaps operator new allocated
memory from some other heap, or some other place that the default operator
new does not know about. Then, how is default operator delete supposed to
know that memory was allocated from here? So, to keep things sane, we
should do that too.


Regards,
-Dhruv.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Rajaram S. Gaunker
Guest





PostPosted: Thu Dec 25, 2003 7:12 am    Post subject: Re: for every overloaded new we need to declate a delete wit Reply with quote

"
Quote:
hi all

for every overloaded "operator new" we need to overload "operator
delete" with similar signature.

I've read in D&E that this is not the case but in Scott Mayers book
"Effective c++" i read that u need to do that.
Is it a new change in standrads.

It is not that we NEED to strictly define operator delete for operator new
defined by us, but consider the case where you operator new does something
else apart from memory allocation, or perhaps operator new allocated
memory from some other heap, or some other place that the default operator
new does not know about. Then, how is default operator delete supposed to
know that memory was allocated from here? So, to keep things sane, we
should do that too.

Hi Thanx


I understood why we have to declare a delete but .If we declare 2
operator new, while calling delete how it will be known that which new
is used to do the allocation.

for example we may pass two the object allocate din two different ways
to same function which deletes them in this case how it will be know.

please explain

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Thu Dec 25, 2003 2:41 pm    Post subject: Re: for every overloaded new we need to declate a delete wit Reply with quote

In message <e3c5fdc7.0312242236.2021de88 (AT) posting (DOT) google.com>, Rajaram S.
Gaunker <rajaram (AT) emuzed (DOT) com> writes
Quote:
Hi Thanx

I understood why we have to declare a delete but .If we declare 2
operator new, while calling delete how it will be known that which new
is used to do the allocation.

for example we may pass two the object allocate din two different ways
to same function which deletes them in this case how it will be know.

please explain


The delete expression always uses the delete operator whose declaration
is delete(void*, size_t). If no such function is supplied by the class
then the global one will be used.

The other delete operators are supplied for the special case when a
dynamically constructed object is being destroyed as part of the process
resulting from a failed construction. At that point the code 'knows'
which operator new was used and selects the matching operator delete if
there is one, else it uses the default.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
or http://www.robinton.demon.co.uk
Happy Xmas, Hanukkah, Yuletide, Winter/Summer Solstice to all.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv
Guest





PostPosted: Thu Dec 25, 2003 5:39 pm    Post subject: Re: for every overloaded new we need to declate a delete wit Reply with quote

On Thu, 25 Dec 2003 02:12:16 -0500, Rajaram S. Gaunker wrote:

Quote:
"
hi all

for every overloaded "operator new" we need to overload "operator
delete" with similar signature.

I've read in D&E that this is not the case but in Scott Mayers book
"Effective c++" i read that u need to do that.
Is it a new change in standrads.

It is not that we NEED to strictly define operator delete for operator new
defined by us, but consider the case where you operator new does something
else apart from memory allocation, or perhaps operator new allocated
memory from some other heap, or some other place that the default operator
new does not know about. Then, how is default operator delete supposed to
know that memory was allocated from here? So, to keep things sane, we
should do that too.

Hi Thanx

I understood why we have to declare a delete but .If we declare 2
operator new, while calling delete how it will be known that which new
is used to do the allocation.

for example we may pass two the object allocate din two different ways
to same function which deletes them in this case how it will be know.

please explain

Two versions of operator new overloaded or rather decalared by the user
can not have the exact same signature. So, you cannot have:

void *operator new (size_t); ___[1]
void *operator new (size_t);

In the same program. Furthermore, an operator delete having matching
parameters should be present for the deallocation to happen correctly.

Considering [1] as the allocation function, this will be the corresponding
deallocation function:

void operator delete (void*);

Similarly, you can have parameterised operator new and operator delete:

struct Foo { };

void *operator new (size_t, Foo *fptr);
void operator delete (void*, Foo*);

Now, you can call parameterized operator delete to select which
deallocation function to use.


Regards,
-Dhruv.




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.