 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Uwe Schnitker Guest
|
Posted: Wed Sep 24, 2003 8:21 pm Post subject: Dangers of overloading global new and delete? |
|
|
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
|
Posted: Fri Sep 26, 2003 10:36 am Post subject: Re: Dangers of overloading global new and delete? |
|
|
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 |
|
 |
|
|
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
|
|