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

  1. Newsgroups: cvnet.c++,comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Finding memory leaks
  5. Message-ID: <9232813.12737@mulga.cs.mu.OZ.AU>
  6. Keywords: OOP,memory leaks
  7. Sender: news@cs.mu.OZ.AU
  8. Organization: Computer Science, University of Melbourne, Australia
  9. References: <1992Nov20.191828.14500@ultb.isc.rit.edu> <1992Nov22.181937.2176@taumet.com>
  10. Date: Mon, 23 Nov 1992 02:47:40 GMT
  11. Lines: 42
  12.  
  13. steve@taumet.com (Steve Clamage) writes:
  14.  
  15. >mpk9172@ultb.isc.rit.edu (M.P. Kirby) writes:
  16. >
  17. >>In C I used to use a memory leak finder that would help me find
  18. >>my memory leaks.  I basically created a #define for malloc that redefined
  19. >>it ...
  20. >
  21. >>I would like to do the same kind of thing in C++.  Unfortunately, it
  22. >>seems that new is not as easy to #define over as malloc.  I thought
  23. >>(briefly) that I could do something like #define new new(__LINE__,__FILE__) 
  24. >> and then overload the new operator, but this causes a recursive macro.
  25. >
  26. >There have been two answers to this question that completely missed
  27. >the point.  You do NOT want to replace "new" with a macro.  "new" is
  28. >a keyword which causes constructors to be called, and memory to be
  29. >allocated via the function "operator new()".  If you replace the "new"
  30. >keyword with a macro, you destroy the semantics of most programs.
  31.  
  32. Unfortunately, we DO want to replace "new" with a macro, so that we can
  33. get __LINE__ and __FILE__. Redefining operator new() is not sufficient, because
  34. __LINE__ inside operator new() refers to the line where operator new() was
  35. redefined, not the line where new was invoked.
  36.  
  37. The problem is not that "new" causes constructors to be called: we can solve
  38. that by invoking "new" from within the macro definition. Neither is the
  39. problem with recursive macros: as noted previously, they are quite OK.
  40. The problem is that the macro preprocessor is not capable of replacing
  41. all uses of keyword "new" which will cause calls to "::operator new()"
  42. without also affecting other uses of the "new" keyword.
  43. (Vector new and delete might also cause some other problems.)
  44.  
  45. The only way that I can see of solving the problem is to write a preprocessor
  46. which properly parses the source code, and then does the desired replacements.
  47. I don't see how it could be done using just the simple textual substitution
  48. provided by the C preprocessor.
  49.  
  50. -- 
  51. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  52. This .signature virus is a self-referential statement that is true - but 
  53. you will only be able to consistently believe it if you copy it to your own
  54. .signature file!
  55.