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 

Dangers of overloading global new and delete?

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





PostPosted: Wed Sep 24, 2003 8:21 pm    Post subject: Dangers of overloading global new and delete? Reply with quote



I seem to remember that the probably most prominent advice regarding
custom memory management in C++ - ignoring "Don't do it!" and "Don't
do it yet! " for a moment - is to avoid overloading the global
operator new and operator delete functions.

However, rereading (skimming) through Scott Meyers and Herb Sutters
books I find out that they describe overloading these functions
class-specific, but don't really bother about the global one's.
Their books only describe the interactions between the class-specific
and global functions.

Question: Are there really important aguments against overloading the
operator new function (and consorts) as one part of a custom memory
management scheme? (Apart from buy-vs.-build considerations, which
we do take into account.)

Regards,

Uwe

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





PostPosted: Fri Sep 26, 2003 10:36 am    Post subject: Re: Dangers of overloading global new and delete? Reply with quote



Uwe-

The decision to overide global new/delete/et. al. is determined by
what you need to do. The general comment i've seen is that it is
"not for the faint of heart." That said, it is also a bit of a rite of
passage in that you will learn to appreciate and understand a lot
of things about memory systems you didn't before! [ Kinda like
compiling the Linux kernel. kinda OT...] So, thinking about it
and doing it are totally different experiences.

As far as C++ is concerned, the ability to override new/delete
on a per-class basis is a godsend oftentimes. There are a lot of
gotchas though that Meyers & Sutters point out, and I defer to
them.

Regarding gotchas and recommendations for global overrides,
here are a few from personal experience. Watch out for other
libs or headers that are already overriding new/delete. MSoft
libs are pretty aggressive about it, especially MFC projects.
You will probably need some sorts of macros to get proper
overriding working, and this means the ability to toggle your
overrides on and off arbitrarily. This is true, too, if you plan
on using garbage collection.

If you want to see some code that does it, check out the
Game Programming Gems, vol. 1. One of the articles talks
about inserting a memory tracking system that involves doing
just this. [ Ironically, a lot of compilers, VC++ for instance,
already have systems in place to do this and if you override,
you may bang into them. ] Anyway, the article provides full
source on the CD for that book, and may be a useful starting
place.

good luck,
-jon

"Uwe Schnitker" <schnitker (AT) sigma-c (DOT) com> wrote

Quote:
I seem to remember that the probably most prominent advice regarding
custom memory management in C++ - ignoring "Don't do it!" and "Don't
do it yet! " for a moment - is to avoid overloading the global
operator new and operator delete functions.

However, rereading (skimming) through Scott Meyers and Herb Sutters
books I find out that they describe overloading these functions
class-specific, but don't really bother about the global one's.
Their books only describe the interactions between the class-specific
and global functions.

Question: Are there really important aguments against overloading the
operator new function (and consorts) as one part of a custom memory
management scheme? (Apart from buy-vs.-build considerations, which
we do take into account.)



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