本人写了如下一个类
#include<assert.h>
class Matrix
{
private:
int m_cols;
int m_rows;
double** m_data;
public:
Matrix(int rows,int cols);
Matrix();
Matrix( Matrix& objMatrix );
virtual ~Matrix();
void SetVal(int rows,int cols,double val);
double GetVal(int rows,int cols);
int GetRow(){return m_rows;};
int GetCol(){return m_cols;};
Matrix operator*(Matrix& right);
Matrix operator+(Matrix& right);
Matrix operator-(Matrix& right);
Matrix operator-();
Matrix Transpose();
Matrix AthwartMatrix();
};Matrix::Matrix(int rows=1,int cols=1)
{
assert(rows>0 && cols>0);
m_cols=cols;
m_rows=rows;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=0;
}
}Matrix::Matrix()
{
m_cols=1;
m_rows=1;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=0;
}
}Matrix::Matrix(Matrix& objMatrix)
{
m_cols=objMatrix.m_cols;
m_rows=objMatrix.m_rows;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];if(this==&objMatrix)
m_data=objMatrix.m_data;
else{
for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=objMatrix.m_data[i][j];
}
}
}Matrix::~Matrix()
{
/*
for(int i=0;i<m_rows;i++)
delete [] m_data[i];
*/
}void Matrix::SetVal( int rows,int cols,double val )
{
assert(rows>0 && rows<=m_rows);
assert(cols>0 && cols<=m_cols);
m_data[rows-1][cols-1]=val;
}double Matrix::GetVal(int rows,int cols)
{
assert(rows>0 && rows<=m_rows);
assert(cols>0 && cols<=m_cols);
return m_data[rows-1][cols-1];
}Matrix Matrix::operator*( Matrix& right)
{
assert(m_cols==right.m_rows);
double valTemp;
int t;
Matrix objMatrix(m_rows,right.m_cols);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=right.m_cols;cols++)
{
valTemp=0;
t=1;
while(t<=m_cols)
{
valTemp+=GetVal(rows,t)*right.GetVal(t,cols);
t++;
}
objMatrix.SetVal(rows,cols,valTemp);
}
return objMatrix;
}Matrix Matrix::operator+(Matrix& right)
{
assert(m_cols==right.m_cols);
assert(m_rows==right.m_rows);
double valTemp=0;
Matrix objMatrix(m_rows,right.m_cols);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols)+right.GetVal(rows,cols);
objMatrix.SetVal(rows,cols,valTemp);
}
return objMatrix;
}Matrix Matrix::operator-(Matrix& right)
{
assert(m_cols==right.m_cols);
assert(m_rows==right.m_rows);
double valTemp=0;
Matrix objMatrix(m_rows,right.m_cols);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols)-right.GetVal(rows,cols);
objMatrix.SetVal(rows,cols,valTemp);
}
return objMatrix;
}Matrix Matrix::operator-()
{
double valTemp;
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols);
SetVal(rows,cols,-valTemp);
}
return *this;
}Matrix Matrix::Transpose()
{
double valTemp=0;
Matrix objMatrix(m_cols,m_rows);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols);
objMatrix.SetVal(cols,rows,valTemp);
}
return objMatrix;
}在开始时的析构函数如注释中实现,但是发现在vc中运行时,虽然通过编译,但是在到了调用这个类的地方确弹出了系统对话框,出错,然而我把析构函数的内容注释掉后,程序就正常运行,但是结果当然是导致大量的内存泄漏,本人十分不解,到底应该怎样实现析构函数才能使程序正常,请高手指教
#include<assert.h>
class Matrix
{
private:
int m_cols;
int m_rows;
double** m_data;
public:
Matrix(int rows,int cols);
Matrix();
Matrix( Matrix& objMatrix );
virtual ~Matrix();
void SetVal(int rows,int cols,double val);
double GetVal(int rows,int cols);
int GetRow(){return m_rows;};
int GetCol(){return m_cols;};
Matrix operator*(Matrix& right);
Matrix operator+(Matrix& right);
Matrix operator-(Matrix& right);
Matrix operator-();
Matrix Transpose();
Matrix AthwartMatrix();
};Matrix::Matrix(int rows=1,int cols=1)
{
assert(rows>0 && cols>0);
m_cols=cols;
m_rows=rows;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=0;
}
}Matrix::Matrix()
{
m_cols=1;
m_rows=1;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=0;
}
}Matrix::Matrix(Matrix& objMatrix)
{
m_cols=objMatrix.m_cols;
m_rows=objMatrix.m_rows;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];if(this==&objMatrix)
m_data=objMatrix.m_data;
else{
for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=objMatrix.m_data[i][j];
}
}
}Matrix::~Matrix()
{
/*
for(int i=0;i<m_rows;i++)
delete [] m_data[i];
*/
}void Matrix::SetVal( int rows,int cols,double val )
{
assert(rows>0 && rows<=m_rows);
assert(cols>0 && cols<=m_cols);
m_data[rows-1][cols-1]=val;
}double Matrix::GetVal(int rows,int cols)
{
assert(rows>0 && rows<=m_rows);
assert(cols>0 && cols<=m_cols);
return m_data[rows-1][cols-1];
}Matrix Matrix::operator*( Matrix& right)
{
assert(m_cols==right.m_rows);
double valTemp;
int t;
Matrix objMatrix(m_rows,right.m_cols);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=right.m_cols;cols++)
{
valTemp=0;
t=1;
while(t<=m_cols)
{
valTemp+=GetVal(rows,t)*right.GetVal(t,cols);
t++;
}
objMatrix.SetVal(rows,cols,valTemp);
}
return objMatrix;
}Matrix Matrix::operator+(Matrix& right)
{
assert(m_cols==right.m_cols);
assert(m_rows==right.m_rows);
double valTemp=0;
Matrix objMatrix(m_rows,right.m_cols);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols)+right.GetVal(rows,cols);
objMatrix.SetVal(rows,cols,valTemp);
}
return objMatrix;
}Matrix Matrix::operator-(Matrix& right)
{
assert(m_cols==right.m_cols);
assert(m_rows==right.m_rows);
double valTemp=0;
Matrix objMatrix(m_rows,right.m_cols);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols)-right.GetVal(rows,cols);
objMatrix.SetVal(rows,cols,valTemp);
}
return objMatrix;
}Matrix Matrix::operator-()
{
double valTemp;
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols);
SetVal(rows,cols,-valTemp);
}
return *this;
}Matrix Matrix::Transpose()
{
double valTemp=0;
Matrix objMatrix(m_cols,m_rows);
for(int rows=1;rows<=m_rows;rows++)
for(int cols=1;cols<=m_cols;cols++)
{
valTemp=GetVal(rows,cols);
objMatrix.SetVal(cols,rows,valTemp);
}
return objMatrix;
}在开始时的析构函数如注释中实现,但是发现在vc中运行时,虽然通过编译,但是在到了调用这个类的地方确弹出了系统对话框,出错,然而我把析构函数的内容注释掉后,程序就正常运行,但是结果当然是导致大量的内存泄漏,本人十分不解,到底应该怎样实现析构函数才能使程序正常,请高手指教
for(int i=0;i<m_rows;i++)
delete [] m_data[i];delete[] m_data;
Matrix::~Matrix()
{
delete [] m_data[0];
delete [] m_data;
}
不过提点建议,既然是Matrix,那么就把每一行也封装成类,那样条理就清晰了。
//'nrows' 是该数组列数。
//所以合法的列数为 (0, nrows-1) 开区间。
//'ncols[r]' 则是 'r' 列的行数 ('r' 值域为 [0..nrows-1])。
{
Fred** matrix = new Fred*[nrows];
for (unsigned r = 0; r < nrows; ++r)
matrix[r] = new Fred[ ncols[r] ]; //使用 matrix[i][j]... //释放就是配置的反动作:
for (r = nrows; r > 0; --r)
delete [] matrix[r-1];
delete [] matrix;
}
double (*matrix)[ncols] = new double[nrows][ncols];...delete [] matrix;
我是在这样调用这个类的:
void CThreeDView::op( )
{
Matrix *M1 = new Matrix(4,4);
Matrix *M2 = new Matrix(4,4);
Matrix *M3 = new Matrix(4,4);
M2->SetVal(1,1,4);
*M3 = (*M1)*(*M2);
CString s;
s.Format("%d",M3->GetVal(1,1));
pDC->TextOut(100,110,s);
delete M1;
delete M2;
delete M3;
}
编译时没有问题,就是在运行这地方的时候就出现了问题,
Debug Assertion Failed
Program: F:\kkk\ThreeD.exe
File: dbgheap.c
Line: 1017
Expression: _BLOCK_TYPE_ID_VALID(pHead->nBlockUse)For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.
这就是系统框提示中所显示的信息
不解,请指教
结果调用对象拷贝的默认=
直接复制了m_datas指针
书写operator=如下:
operator=(const Matrix& src)
{
if(m_data)
{
for(int i=0;i<m_rows;i++)
if(m_data[i])
delete [] m_data[i];
delete[] m_data;
}
m_cols=objMatrix.m_cols;
m_rows=objMatrix.m_rows;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];
if(this==&objMatrix)
m_data=objMatrix.m_data;
else{
for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=objMatrix.m_data[i][j];
}
}
}
另外析构函数也不严格
{
if(this==&src)
return *this; if(m_data)
{
for(int i=0;i<m_rows;i++)
if(m_data[i])
delete [] m_data[i];
delete[] m_data;
}
m_cols=src.m_cols;
m_rows=src.m_rows;
m_data=new double*[m_rows];
for(int i=0;i<m_rows;i++)
m_data[i] =new double[m_cols];
for(i=0;i<m_rows;i++)
for(int j=0;j<m_cols;j++)
{
m_data[i][j]=src.m_data[i][j];
}
return *this;
}