E
E G
Hi
I have a class that is an expanded version of the following:
class Matrix
{
private:
float **_A;
unsigned _rows,_cols;
public:
Matrix():_A(0),_rows(0),_cols(0){}
Matrix(unsigned const rows,unsigned const cols):_A(0),_rows(0),_cols(0)
{
if(rows>0 && cols>0){
_A=new float*[rows];
if(_A==0)
throw "No Memory Left";
_A[0]=new float[rows*cols];
if(_A[0]==0){
delete[] _A;
_A=0;
throw "No Memory Left";
}
if(rows>1)
for(unsigned i=1;i<rows;i++)
_A=_A[0]+ i*cols;
_rows=rows;
_cols=cols;
memset(_A[0],0,rows*cols*sizeof(Type));
}
}
~Matrix()
{
if(_A != 0){
if(_A[0] != 0)
delete [] _A[0];
delete [] _A;
}
_A=0;
_rows=0;
_cols=0;
}
Matrix & operator=(Matrix const& M)
{
if(this != &M){
if(this->_A!=0){
delete[] this->_A[0];
delete[] this->_A;
_rows=0;
_cols=0;
}
this->_A=0;
this->_rows=M._rows;
this->_cols=M._cols;
this->_A=new float*[this->_rows];
if(this->_A==0){
this->_rows=0;
this->_cols=0;
throw "No Memory Left";
}
this->_A[0]=new float[this->_rows*this->_cols];
if(this->_A[0]==0){
delete[] this->_A;
this->_rows=0;
this->_cols=0;
this->_A=0;
throw "No Memory Left";
}
if(this->_rows>1)
for(unsigned i=1;i<this->_rows;i++)
this->_A=this->_A[0]+ i*this->_cols;
memcpy(this->_A[0],M._A[0],
this->_rows*this->_cols*sizeof(float));
}
return *this;
}
}
when I use this class in the following program:
in main(int argc,char **argv)
{
Matrix *pfM;
{
Matrix fM(4,3);
I have a class that is an expanded version of the following:
class Matrix
{
private:
float **_A;
unsigned _rows,_cols;
public:
Matrix():_A(0),_rows(0),_cols(0){}
Matrix(unsigned const rows,unsigned const cols):_A(0),_rows(0),_cols(0)
{
if(rows>0 && cols>0){
_A=new float*[rows];
if(_A==0)
throw "No Memory Left";
_A[0]=new float[rows*cols];
if(_A[0]==0){
delete[] _A;
_A=0;
throw "No Memory Left";
}
if(rows>1)
for(unsigned i=1;i<rows;i++)
_A=_A[0]+ i*cols;
_rows=rows;
_cols=cols;
memset(_A[0],0,rows*cols*sizeof(Type));
}
}
~Matrix()
{
if(_A != 0){
if(_A[0] != 0)
delete [] _A[0];
delete [] _A;
}
_A=0;
_rows=0;
_cols=0;
}
Matrix & operator=(Matrix const& M)
{
if(this != &M){
if(this->_A!=0){
delete[] this->_A[0];
delete[] this->_A;
_rows=0;
_cols=0;
}
this->_A=0;
this->_rows=M._rows;
this->_cols=M._cols;
this->_A=new float*[this->_rows];
if(this->_A==0){
this->_rows=0;
this->_cols=0;
throw "No Memory Left";
}
this->_A[0]=new float[this->_rows*this->_cols];
if(this->_A[0]==0){
delete[] this->_A;
this->_rows=0;
this->_cols=0;
this->_A=0;
throw "No Memory Left";
}
if(this->_rows>1)
for(unsigned i=1;i<this->_rows;i++)
this->_A=this->_A[0]+ i*this->_cols;
memcpy(this->_A[0],M._A[0],
this->_rows*this->_cols*sizeof(float));
}
return *this;
}
}
when I use this class in the following program:
in main(int argc,char **argv)
{
Matrix *pfM;
{
Matrix fM(4,3);