home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!gatech!concert!uvaarpa!murdoch!virginia.edu!gs4t
- From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
- Subject: Re: Matrix Addition Was Re: Complex Addition.
- Message-ID: <1992Dec22.194414.8594@murdoch.acc.Virginia.EDU>
- Keywords: operator, memory leak
- Sender: usenet@murdoch.acc.Virginia.EDU
- Organization: University of Virginia
- References: <1992Dec11.232954.7071@borland.com> <36223@sophia.inria.fr> <1992Dec17.204049.20009@virginia.edu> <1h5s1fINNa65@function.mps.ohio-state.edu>
- Date: Tue, 22 Dec 1992 19:44:14 GMT
- Lines: 67
-
- ren@function.mps.ohio-state.edu (Liming Ren) writes:
- : >class Matrix {
- : > struct MatRep {
- : > int ref;
- : > int rows, cols;
- : > double* d;
- : >
- : > MatRep (int r, int c)
- : > : ref(1), rows(r), cols(c), d(new double[rows*cols]) {}
- : > ~MatRep () { delete [] d; }
- : > }* rep;
- : >public:
- : > Matrix (int r, int c): rep(new MatRep(r,c)) {}
- : > Matrix (const Matrix& m): rep (m.rep) { ++rep->ref; }
- : > ~Matrix () { if (--rep->ref == 0) delete rep; }
- : >
- : > double operator () (int i, int j) { return rep->d[i*rep->rows+j]; }
- : > Matrix& operator = (const Matrix& m) {
- : > if (this != &m) {
- : > this->Matrix::~Matrix (); // self destruct
- : > rep = m.rep; ++rep->ref;
- : > }
- : > return *this;
- : > }
- : >
- : > friend Matrix operator + (const Matrix& a, const Matrix& b);
- : >};
- : >
- : >// note the return by value here
- : >Matrix operator + (const Matrix& a, const Matrix& b)
- : >{
- : > Matrix sum (a.rep->rows, a.rep->cols);
- : > for (int i=0; i < sum.rep->rows * sum.rep->cols; i++)
- : > sum.rep->d[i] = a.rep->d[i] + b.rep->d[i];
- : > return sum;
- : >}
- : I am new to c++, my questions are:
-
- There are two very well written introductory books on
- C++ that new users may want to read:
-
- B. Stroustrup, "The C++ Programming Language, 2nd Edition,"
- Reading, MA: Addison-Wesley, 1991.
-
- S. Lippman, "The C++ Primer, 2nd Edition,"
- Reading, MA: Addison-Wesley, 1992.
-
- : (1) In the destructor ~Matrix(), what happens to the memory pointed by d? It is my understanding
- : that the memory pointed by d should also be deleted explicitly. Although I am not quite sure.
-
- [As an aside please keep your lines less than 72 chars
- long in the future. Thanks.]
-
- ~Matrix deletes its rep iff no other Matrix object is
- referring to it. When rep is deleted, the destructor
- ~MatRep takes care of deleting the memory pointed to by d.
-
- :
- : (2)In the operator + definition, sum.ref is initialized to 1. In order to be alive after exiting
- : the function, the sum.ref at least should be 2. Where is the ref incremented exactly and by which
- : function?
-
- sum will be destructed when operator + goes out of scope.
- But, before that when operator + returns, sum's rep->ref
- is incremented by the copy constructor Matrix(const Matrix&)
- and hence, sum's rep will not be deleted when sum is destructed.
-
-