home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18323 < prev    next >
Encoding:
Text File  |  1992-12-22  |  2.8 KB  |  80 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!gatech!concert!uvaarpa!murdoch!virginia.edu!gs4t
  3. From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
  4. Subject: Re: Matrix Addition  Was Re: Complex Addition.
  5. Message-ID: <1992Dec22.194414.8594@murdoch.acc.Virginia.EDU>
  6. Keywords: operator, memory leak
  7. Sender: usenet@murdoch.acc.Virginia.EDU
  8. Organization: University of Virginia
  9. References: <1992Dec11.232954.7071@borland.com> <36223@sophia.inria.fr> <1992Dec17.204049.20009@virginia.edu> <1h5s1fINNa65@function.mps.ohio-state.edu>
  10. Date: Tue, 22 Dec 1992 19:44:14 GMT
  11. Lines: 67
  12.  
  13. ren@function.mps.ohio-state.edu (Liming Ren) writes:
  14. : >class Matrix {
  15. : >    struct MatRep {
  16. : >        int    ref;
  17. : >        int      rows, cols;
  18. : >        double*    d;
  19. : >    
  20. : >        MatRep (int r, int c)
  21. : >        : ref(1), rows(r), cols(c), d(new double[rows*cols]) {}
  22. : >        ~MatRep () { delete [] d; }
  23. : >    }* rep;
  24. : >public:
  25. : >    Matrix (int r, int c): rep(new MatRep(r,c)) {}
  26. : >    Matrix (const Matrix& m): rep (m.rep) { ++rep->ref; }
  27. : >    ~Matrix () { if (--rep->ref == 0) delete rep; }
  28. : >
  29. : >    double    operator () (int i, int j) { return rep->d[i*rep->rows+j]; }
  30. : >    Matrix& operator =  (const Matrix& m) {
  31. : >        if (this != &m) {
  32. : >            this->Matrix::~Matrix (); // self destruct
  33. : >            rep = m.rep; ++rep->ref;
  34. : >        }
  35. : >        return *this;
  36. : >    }
  37. : >
  38. : >    friend Matrix  operator + (const Matrix& a, const Matrix& b);
  39. : >};
  40. : >
  41. : >// note the return by value here
  42. : >Matrix operator + (const Matrix& a, const Matrix& b)
  43. : >{
  44. : >    Matrix sum (a.rep->rows, a.rep->cols);
  45. : >    for (int i=0; i < sum.rep->rows * sum.rep->cols;  i++)
  46. : >        sum.rep->d[i] = a.rep->d[i] + b.rep->d[i];
  47. : >    return sum;
  48. : >}
  49. :  I am new to c++, my questions are:
  50.  
  51. There are two very well written introductory books on
  52. C++ that new users may want to read:
  53.  
  54. B. Stroustrup, "The C++ Programming Language, 2nd Edition,"
  55. Reading, MA: Addison-Wesley, 1991.
  56.  
  57. S. Lippman, "The C++ Primer, 2nd Edition,"
  58. Reading, MA: Addison-Wesley, 1992.
  59.  
  60. : (1) In the destructor ~Matrix(), what happens to the memory pointed by d? It is my understanding
  61. : that the memory pointed by d should also be deleted explicitly. Although I am not quite sure.
  62.  
  63. [As an aside please keep your lines less than 72 chars
  64.  long in the future. Thanks.]
  65.  
  66. ~Matrix deletes its rep iff no other Matrix object is
  67. referring to it. When rep is deleted, the destructor
  68. ~MatRep takes care of deleting the memory pointed to by d.
  69.  
  70. : (2)In the operator + definition,  sum.ref is initialized to 1. In order to be alive after exiting
  71. : the function, the sum.ref at least should be 2. Where is the ref incremented exactly and by which
  72. : function?
  73.  
  74. sum will be destructed when operator + goes out of scope.
  75. But, before that when operator + returns, sum's rep->ref
  76. is incremented by the copy constructor Matrix(const Matrix&)
  77. and hence, sum's rep will not be deleted when sum is destructed.
  78.  
  79.