home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: cvnet.c++,comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Finding memory leaks
- Message-ID: <1992Nov22.181937.2176@taumet.com>
- Keywords: OOP,memory leaks
- Organization: TauMetric Corporation
- References: <1992Nov20.191828.14500@ultb.isc.rit.edu>
- Date: Sun, 22 Nov 1992 18:19:37 GMT
- Lines: 30
-
- mpk9172@ultb.isc.rit.edu (M.P. Kirby) writes:
-
- >In C I used to use a memory leak finder that would help me find
- >my memory leaks. I basically created a #define for malloc that redefined
- >it ...
-
- >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 have been two answers to this question that completely missed
- the point. You do NOT want to replace "new" with a macro. "new" is
- a keyword which causes constructors to be called, and memory to be
- allocated via the function "operator new()". If you replace the "new"
- keyword with a macro, you destroy the semantics of most programs.
-
- You are allowed in C++ to replace "operator new()" and "operator delete()"
- with your own versions. Since these are the functions which actually do
- the memory allocation, you can get any effect you want by replacing them.
- (If you replace one, you should replace both, to ensure compatibility.)
- You don't even have to recompile any code -- just re-link your program.
- The ARM and Stroustrup 2 explain how to do this, apart from other books
- and many magazine articles which also cover the subject. It takes a bit
- of explaining to cover all the subtle points which you have to consider,
- so I prefer not to present just a simplistic answer here.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-