home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16717 < prev    next >
Encoding:
Text File  |  1992-11-21  |  2.2 KB  |  57 lines

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