home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18147 < prev    next >
Encoding:
Internet Message Format  |  1992-12-17  |  3.7 KB

  1. Path: sparky!uunet!gatech!prism!federation!andy
  2. From: andy@federation.gatech.edu (Andy Register)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Matrix Addition  Was Re: Complex Addition.
  5. Keywords: operator, memory leak
  6. Message-ID: <78524@hydra.gatech.EDU>
  7. Date: 17 Dec 92 20:03:53 GMT
  8. References: <1992Dec11.170849.18525@rs6000.bham.ac.uk> <1992Dec11.232954.7071@borland.com> <36223@sophia.inria.fr>
  9. Sender: news@prism.gatech.EDU
  10. Organization: CERL-EE, Georgia Institue of Technology
  11. Lines: 65
  12.  
  13. In article <36223@sophia.inria.fr> gchow@hawai.inria.fr (Gloria Chow) writes:
  14.  >In article <1992Dec11.232954.7071@borland.com>, pete@borland.com (Pete Becker) writes:
  15.  >|> In article <1992Dec11.170849.18525@rs6000.bham.ac.uk> pickerig@eee.bham.ac.uk (Guy Pickering) writes:
  16.  >|> >In ARM, the code for the `+` operator for the complex class is given as:
  17.  >
  18.  >class MATRIX
  19.  >{
  20.  >protected:
  21.  >    double      **entry;
  22.  >    int         nrows, ncols;
  23.  >public:
  24.  >
  25.  >the "+" operation works and sum contains the proper matrix, that is until
  26.  >"+" finishes and goes out of scope.  The "return" statement makes a temporary
  27.  >copy of sum (ie. the values of its nrows, ncols, and the pointer entry), and
  28.  >then proceed to destroy sum automatically.  So, now the entry pointer of this
  29.  >temporary is no longer pointing to a valid block of memory!!   
  30.  >
  31.  >Is this really what happened?  I am new to C++, I am not sure my interpretation
  32.  >is correct.  But all I can tell is that this simple addition and assignment 
  33.  >sequence DOESN'T work, and I don't know how to get around it.  HELP!!
  34.  >
  35.  >gloria
  36.  
  37. In general when you have a pointer in your class data you have to be
  38. especially careful or the situation you describe will occur.  Also, if your
  39. constructor calls new() then the following is probably appropriate advice.
  40.  
  41. There are two ways to correct the problem you are experiencing:  make the
  42. class a concrete data type or use reference counting.
  43.  
  44. To make the class concrete you have to either, be *positive* that the
  45. compiler supplied copy constructor and operator= functions are appropriate
  46. for the data in your class or you have to supply these functions so that
  47. the proper behavior results.  Almost always, certainly this case,
  48. when your data includes a pointer you have to explicitly write a copy
  49. constructor and an operator= method.  The copy constructor acts just
  50. like the default constructor (i.e. allocates new storage) except that
  51. it also performs a deep copy of the data items being pointed to.  The
  52. operator= method should also performs a deep copy.  Note that a shallow
  53. copy, only the pointer and not the underlying data copied, is the default.
  54. All this extra stuff gets delete'ed when the temp goes out of scope and the
  55. destructor is called.
  56.  
  57. To use reference counting, you also have to supply your own copy ctor
  58. and operator=.  In this case, a true copy is not made, only a reference
  59. to some original memory location.  When you say x=y, a shallow copy is
  60. made, that is the pointer in x points to the same location as the pointer
  61. in y but a reference count in y is incremented.  Same with copy ctor.
  62. When the variable goes out of scope, the dtor is called. The reference
  63. count is decremented and if the reference count is zero, the memory
  64. is delete'ed.  There are some twists in the class data since all
  65. objects that reference the same memory location must have access to 
  66. this shared reference count but that is pretty easy ot accomplish.
  67.  
  68. My advice is to make the class concrete by providing deep copy
  69. in the copy ctor and operator=.  This is the easiest to understand.  Later
  70. when you have experience on your side (and a couple of good reference
  71. books!) you can have a go at the reference counting idiom.  Coplien
  72. and Eckel both cover this topic.
  73.  
  74. I hope I got that right, but if I did not, we will know soon!  (grin)
  75.  
  76. Toodles
  77. Andy
  78.