home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!caen!uvaarpa!murdoch!virginia.edu!gs4t
- From: gs4t@virginia.edu (Gnanasekaran Swaminathan)
- Subject: Re: Matrix Addition Was Re: Complex Addition.
- Message-ID: <1992Dec17.204049.20009@murdoch.acc.Virginia.EDU>
- Keywords: operator, memory leak
- Sender: usenet@murdoch.acc.Virginia.EDU
- Organization: University of Virginia
- References: <1992Dec11.170849.18525@rs6000.bham.ac.uk> <1992Dec11.232954.7071@borland.com> <36223@sophia.inria.fr>
- Date: Thu, 17 Dec 1992 20:40:49 GMT
- Lines: 72
-
- gchow@hawai.inria.fr (Gloria Chow) writes:
- : MATRIX& MATRIX::operator+( const MATRIX& addend )
- : {
- : register int i,j;
- : MATRIX sum(nrows, ncols);
- :
- : for( i=0 ; i<nrows ; i++ )
- : for ( j=0 ; j<ncols ; j++ )
- : sum.entry[i][j] = entry[i][j] + addend.entry[i][j];
- :
- : return sum;
- : }
- :
- :
- : Okay, the problem is when I tried to use the "+" operator as follows:
- :
- : matrix_c = matrix_a + matrix_b;
- :
- : the "+" operation works and sum contains the proper matrix, that is until
- : "+" finishes and goes out of scope. The "return" statement makes a temporary
- : copy of sum (ie. the values of its nrows, ncols, and the pointer entry), and
- : then proceed to destroy sum automatically. So, now the entry pointer of this
- : temporary is no longer pointing to a valid block of memory!!
-
- You are returning a reference to a local variable sum.
- That is, no temporary copy is made in your case.
- So, when the function operator+ goes out of scope, sum
- gets destroyed and hence, the reference refers to junk.
-
- One technique advocated several times in this forum and
- by several good books is to use reference counting and
- return by value in case of big objects like Matrix.
-
- For example,
-
- 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;
- }
-
-
-