home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18293 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  2.2 KB

  1. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!not-for-mail
  2. From: ren@function.mps.ohio-state.edu (Liming Ren)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Matrix Addition  Was Re: Complex Addition.
  5. Date: 21 Dec 1992 20:46:23 -0500
  6. Organization: Department of Mathematics, The Ohio State University
  7. Lines: 56
  8. Message-ID: <1h5s1fINNa65@function.mps.ohio-state.edu>
  9. References: <1992Dec11.232954.7071@borland.com> <36223@sophia.inria.fr> <1992Dec17.204049.20009@murdoch.acc.Virginia.EDU>
  10. NNTP-Posting-Host: function.mps.ohio-state.edu
  11. Keywords: operator, memory leak
  12.  
  13.  
  14. In article <1992Dec17.204049.20009@murdoch.acc.Virginia.EDU> gs4t@virginia.edu (Gnanasekaran Swaminathan) writes:
  15. >One technique advocated several times in this forum and
  16. >by several good books is to use reference counting and 
  17. >return by value in case of big objects like Matrix.
  18. >
  19. >For example,
  20. >
  21. >class Matrix {
  22. >    struct MatRep {
  23. >        int    ref;
  24. >        int      rows, cols;
  25. >        double*    d;
  26. >    
  27. >        MatRep (int r, int c)
  28. >        : ref(1), rows(r), cols(c), d(new double[rows*cols]) {}
  29. >        ~MatRep () { delete [] d; }
  30. >    }* rep;
  31. >public:
  32. >    Matrix (int r, int c): rep(new MatRep(r,c)) {}
  33. >    Matrix (const Matrix& m): rep (m.rep) { ++rep->ref; }
  34. >    ~Matrix () { if (--rep->ref == 0) delete rep; }
  35. >
  36. >    double    operator () (int i, int j) { return rep->d[i*rep->rows+j]; }
  37. >    Matrix& operator =  (const Matrix& m) {
  38. >        if (this != &m) {
  39. >            this->Matrix::~Matrix (); // self destruct
  40. >            rep = m.rep; ++rep->ref;
  41. >        }
  42. >        return *this;
  43. >    }
  44. >
  45. >    friend Matrix  operator + (const Matrix& a, const Matrix& b);
  46. >};
  47. >
  48. >// note the return by value here
  49. >Matrix operator + (const Matrix& a, const Matrix& b)
  50. >{
  51. >    Matrix sum (a.rep->rows, a.rep->cols);
  52. >    for (int i=0; i < sum.rep->rows * sum.rep->cols;  i++)
  53. >        sum.rep->d[i] = a.rep->d[i] + b.rep->d[i];
  54. >    return sum;
  55. >}
  56.  I am new to c++, my questions are:
  57. (1) In the destructor ~Matrix(), what happens to the memory pointed by d? It is my understanding
  58. that the memory pointed by d should also be deleted explicitly. Although I am not quite sure.
  59.  
  60. (2)In the operator + definition,  sum.ref is initialized to 1. In order to be alive after exiting
  61. the function, the sum.ref at least should be 2. Where is the ref incremented exactly and by which
  62. function?
  63.  
  64. Thanks for your help!
  65.  
  66.  
  67.  
  68.  
  69.