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 

Can you overload operator delete?

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





PostPosted: Sat Oct 18, 2003 11:14 pm    Post subject: Can you overload operator delete? Reply with quote



Hello,

I know that operator new can be overload, e.g. as follows:

void* operator new( size_t size, const char* file, int line );

But how is it with operator delete? If I declare

void operator delete( void* p, const char* file, int line );

how do I make the compiler use it?

int main()
{
int* p = new( __FILE__, __LINE__ ) int; // Works fine

delete( __FILE__, __LINE__ ) p; // Error

return 0;
}

VC++ 6.0 rejects the call to operator delete from above - what can I do?

Regards,

Matthias





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





PostPosted: Sun Oct 19, 2003 10:32 am    Post subject: Re: Can you overload operator delete? Reply with quote



Hello!

Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:

Quote:
Hello,

I know that operator new can be overload, e.g. as follows:

void* operator new( size_t size, const char* file, int line );

But how is it with operator delete? If I declare

void operator delete( void* p, const char* file, int line );

how do I make the compiler use it?


You cannot pass arguments when using the operator syntax.
But you can call it as a function:

int *p = new int (__FILE__, __LINE__);
operator delete (p, __FILE__, __LINE__);

Normally, you *only* overload the delete operator to match an
existing new operator. This is done for exception safety: If
the constructor for an object being created by "operator new"
throws an exception, the memory for the object is /only/ released
if a matching and accessible "operator delete" declaration is
found.

Note that your declaration of "delete" does not match your
"new" operator, because it is missing "size_t" as the second
parameter. See 5.3.4/19 and /20.

The reason why you typically don't want to overload operator
delete for any other reason as mentioned above is that you
probably don't have much information about the object to be
destroyed at destruction time. That means that you typically
don't know how the object has been created, so how you are
supposed to choose the "right" deallocation operator? This
is why destructors in C++ don't have parameters: the caller
of a destructor (using operator delete) is not expected to
know the complete type of the object which is destroyed (think
of polymorphic use where you only have a pointer to a base
class), so how can the caller be expected to be able to supply
the "right" destructor arguments?


Regards,
Christoph



[ 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: Sun Oct 19, 2003 6:41 pm    Post subject: Re: Can you overload operator delete? Reply with quote



On Sat, 18 Oct 2003 19:14:09 -0400, Matthias Hofmann wrote:

Quote:
Hello,
I know that operator new can be overload, e.g. as follows:
void* operator new( size_t size, const char* file, int line );
But how is it with operator delete? If I declare
void operator delete( void* p, const char* file, int line );
how do I make the compiler use it?

int main()
{
int* p = new( __FILE__, __LINE__ ) int; // Works fine

//Destroy contents of p if it is of a non-trivial or Non-Pod type.
operator delete(p, __FILE__, __LINE__ );

Quote:
return 0;
}

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
Matthias Hofmann
Guest





PostPosted: Mon Oct 20, 2003 9:06 pm    Post subject: Re: Can you overload operator delete? Reply with quote


Christoph Schulz <kristov (AT) arcor (DOT) de> schrieb in im Newsbeitrag:
bmt843$pkmau$1 (AT) ID-175807 (DOT) news.uni-berlin.de...
Quote:
Hello!

Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:

Hello,

I know that operator new can be overload, e.g. as follows:

void* operator new( size_t size, const char* file, int line );

But how is it with operator delete? If I declare

void operator delete( void* p, const char* file, int line );

how do I make the compiler use it?


You cannot pass arguments when using the operator syntax.
But you can call it as a function:

int *p = new int (__FILE__, __LINE__);
operator delete (p, __FILE__, __LINE__);

Normally, you *only* overload the delete operator to match an
existing new operator. This is done for exception safety: If
the constructor for an object being created by "operator new"
throws an exception, the memory for the object is /only/ released
if a matching and accessible "operator delete" declaration is
found.


Oh, I see. I have to provide an additional

void operator delete( void* );

Quote:
Note that your declaration of "delete" does not match your
"new" operator, because it is missing "size_t" as the second
parameter. See 5.3.4/19 and /20.


This section tells me that the operators match if all arguments except for
the first one are identical. Therefore, I think that my declaration of
"delete" actually does match my "new" operator.

Regards,

Matthias




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

Back to top
Christoph Schulz
Guest





PostPosted: Tue Oct 21, 2003 3:45 pm    Post subject: Re: Can you overload operator delete? Reply with quote

Hello!

Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:

Quote:
Christoph Schulz <kristov (AT) arcor (DOT) de> schrieb in im Newsbeitrag:
bmt843$pkmau$1 (AT) ID-175807 (DOT) news.uni-berlin.de...
Note that your declaration of "delete" does not match your
"new" operator, because it is missing "size_t" as the second
parameter. See 5.3.4/19 and /20.


This section tells me that the operators match if all arguments
except for the first one are identical. Therefore, I think that my
declaration of "delete" actually does match my "new" operator.

You're right. The special handling of the size_t parameter does only
apply to non-placement deallocation functions. Sorry for the wrong
information I gave you.

Regards,
Christoph



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

Back to top
Matthias Hofmann
Guest





PostPosted: Wed Oct 22, 2003 6:39 pm    Post subject: Re: Can you overload operator delete? Reply with quote


Christoph Schulz <kristov (AT) arcor (DOT) de> schrieb in im Newsbeitrag:
bn1j2c$s7v02$1 (AT) ID-175807 (DOT) news.uni-berlin.de...
Quote:
Hello!

Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:

Christoph Schulz <kristov (AT) arcor (DOT) de> schrieb in im Newsbeitrag:
bmt843$pkmau$1 (AT) ID-175807 (DOT) news.uni-berlin.de...
Note that your declaration of "delete" does not match your
"new" operator, because it is missing "size_t" as the second
parameter. See 5.3.4/19 and /20.


This section tells me that the operators match if all arguments
except for the first one are identical. Therefore, I think that my
declaration of "delete" actually does match my "new" operator.

You're right. The special handling of the size_t parameter does only
apply to non-placement deallocation functions. Sorry for the wrong
information I gave you.


What special handling do you mean? Can you point me to the corresponding
section in the standard?

Regards,

Matthias



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

Back to top
Christoph Schulz
Guest





PostPosted: Thu Oct 23, 2003 11:21 am    Post subject: Re: Can you overload operator delete? Reply with quote

Hello!

Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:

Quote:
Christoph Schulz <kristov (AT) arcor (DOT) de> schrieb in im Newsbeitrag:
bn1j2c$s7v02$1 (AT) ID-175807 (DOT) news.uni-berlin.de...
Hello!

Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:

Christoph Schulz <kristov (AT) arcor (DOT) de> schrieb in im Newsbeitrag:
bmt843$pkmau$1 (AT) ID-175807 (DOT) news.uni-berlin.de...
Note that your declaration of "delete" does not match your
"new" operator, because it is missing "size_t" as the second
parameter. See 5.3.4/19 and /20.


This section tells me that the operators match if all arguments
except for the first one are identical. Therefore, I think that my
declaration of "delete" actually does match my "new" operator.

You're right. The special handling of the size_t parameter does only
apply to non-placement deallocation functions. Sorry for the wrong
information I gave you.


What special handling do you mean? Can you point me to the
corresponding section in the standard?


I mean only that

operator delete (void*, size_t)

matches

operator new (size_t)

although the parameter lists are not the same (excluding the first
parameter). This is because "any non-placement deallocation matches
a non-placement allocation function" (5.3.4/19).


Regards,
Christoph



[ 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.