c++ - Deallocating Dynamic 2D Array with Template -
c++ - Deallocating Dynamic 2D Array with Template -
i having error in c++ application having problem debugging. have looked online, , appear doing of allocation/deallocation right way. here code:
template <typename t> class matrix { private: int _rows; int _cols; t** _matrix; public: matrix(int r, int c); ~matrix(); t getvalue(int r, int c); }; template <typename t> matrix<t>::matrix(int r, int c) { _rows = r; _cols = c; _matrix = new t*[_rows]; for(int = 0; < _rows; i++) _matrix[i] = new t[_cols]; for(int = 0; < _rows; i++) for(int j = 0; j < _cols; j++) _matrix[i][j] = null; } template <typename t> matrix<t>::~matrix() { for(int = 0; < _rows; i++) delete [] _matrix[i]; delete [] _matrix; } template <typename t> t matrix<t>::getvalue(int r, int c) { if(r < 0 || r >= _rows || c < 0 || c > _cols) { throw -1; homecoming null; } homecoming _matrix[r][c]; }
and client code...
int main() { matrix<int> mymatrix(3, 3); mymatrix.getvalue(1, 1); // mymatrix.~matrix(); // don't anymore }
as variable "mymatrix" goes out of scope, error:
unhandled exception @ 0x103159da (msvcr1000d.dll)...access violation reading location 0xfeeefee2. , brought file "dbgdel.cpp" _asserte(_block_type_is_valid(phead->nblockuse));
please help!
edit:
okay, neglected give information. please see below:
i have additional method called "t dot(matrix)" i've got 2 methods called "columns()" , "rows()" getters _cols , _rows. , method called "setvalue(int r, int c, t value)" sets _matrix[r][c] = value
. don't think showing implementation necessary these.
template <typename t> t matrix<t>::dot(matrix<t> m) { if(_cols > 1 || m.columns() > 1 || _rows != m.rows()) { throw -1; homecoming null; } t value = 0; for(int = 0; < _rows; i++) { value += _matrix[i][0] * m.getvalue(i, 0); } homecoming value; // whoops, here, forgot type }
and client...
int main() { matrix<int> intm1(3, 1); matrix<int> intm2(3, 1); intm1.setvalue(0, 0, 1); intm1.setvalue(1, 0, 1); intm1.setvalue(2, 0, 1); intm2.setvalue(0, 0, 1); intm2.setvalue(1, 0, 1); intm2.setvalue(2, 0, 1); std::cout << intm1.dot(intm2) << endl; }
this generates same error above, when "dot()" function called.
with edit, issue here still lack of re-create constructor , assignment operator (see reply lastly question). problem when pass matrix
value function, re-create constructor invoked create copy, since haven't defined 1 c++ utilize default re-create constructor, makes shallow copy. consequently, you'll end getting new matrix
shares pointer same elements old matrix
. when new matrix
goes out of scope, destructor fire, cleaning array used other matrix
. when original matrix
goes out of scope , cleaned up, seek delete already-deleted array, causing crash.
to prepare this, need implement re-create constructor , assignment operator correctly duplicate resources. there's rule-of-thumb called rule of 3 says if have destructor, need re-create constructor , assignment operator prevent these sorts of bugs. seek implementing these missing functions , see if problem clears up.
c++ arrays memory-management dynamic
Comments
Post a Comment