home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!munnari.oz.au!metro!otc!swdev!grahamd
- From: grahamd@swdev.research.otca.oz.au (Graham Dumpleton)
- Newsgroups: comp.lang.c++
- Subject: Re: Finding memory leaks
- Keywords: OOP,memory leaks
- Message-ID: <6790@otc.otca.oz>
- Date: 21 Nov 92 23:29:37 GMT
- References: <1992Nov20.191828.14500@ultb.isc.rit.edu> <1992Nov21.145321.2297@ulrik.uio.no>
- Sender: news@otc.otca.oz
- Organization: Technology Development Group, OTC Australia
- Lines: 44
-
- In article <1992Nov21.145321.2297@ulrik.uio.no>, bosnes@boeygen.nr.no (Vidar Bosnes) writes:
- > Mike Kirby (mpk9172@ultb.isc.rit.edu) writes:
- > > I would like to do the same kind of thing in C++. Unfortunately, it
- > > seems that new is not as easy to #define over as malloc. I thought
- > > (briefly) that I could do something like #define new new(__LINE__,__FILE__)
- > > and then overload the new operator, but this causes a recursive macro.
- >
- > There should be no problem in using the macro you suggested. The ARM
- > (chap 16.3.3), Preprocessing; rescanning and further replacement, reads:
- >
- > After all parameters in the replacement list have been replaced, the
- > resulting list is rescanned for more macros to replace. If the name
- > of the macro being replaced is found during this scan or during
- > subsequent rescanning, it is not replaced.
- >
- > Thus, there is no recursion involved. I have used this technique
- > successfully with g++ and dec-cxx. If you use g++, note that this
- > compiler uses non-standard syntax for the call to the overloaded
- > new-operator (curly braces instead of parentheses).
-
- There is a problem with using this approach if you define your own operator
- new at either global scope or for a class. For example consider the following
- code
-
- #define new new(__LINE__,__FILE__)
-
- class Foo
- {
- public:
- void* operator new(size_t);
- };
-
- When this is expanded by the preprocessor you will end up with the operator
- new in the class output as
-
- void* operator new(42,"??")(size_t);
-
- which isn't going to compile.
-
- Similarly you cannot do this for operator delete for the above reasons, although
- there are other reasons why it cant be done for delete also.
-
- --
- Graham Dumpleton (grahamd@swdev.research.otca.oz.au)
-