Here is a very abridged excerpt from a simple matrix manipulation library I'm writing, mostly as a practice project.

Code:

class matrix {
public:
double **a;
int row, col;
matrix (){};
matrix (double *,int,int);
matrix & operator = (const matrix &);
};
//constructor for matrix.
matrix::matrix(double *input, int inrow, int incol) {
row = inrow;
col = incol;
a = new double*[row];
for(int i=0;i<row;i++)
a[i] = new double[col];
if(input != NULL)
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
a[i][j] = *(input + (col*i) +j);
else
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
a[i][j] = 0;
}
//Overloaded = operator, simply checks matrices are of equal size and then
//copies elements from right to left.
matrix & matrix::operator = (const matrix &rhs) {
if(this == &rhs)
return (*this); //not strictly necessary, I know.
if(row == rhs.row && col == rhs.col){
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
a[i][j]= rhs.a[i][j];
return (*this);
}
}

If I was to write this:

Code:

matrix test1 (some_array, rows, columns)
matrix test2 = test1; //here
matrix test3;
test3 = test1;//here

am I writing into memory I don't own?

It seems to me the the default constructor is called, which includes no memory allocation, and then I'm assigning data //here into memory I don't own.

However, I don't think I really understand the whole constructor business, and I don't get any errors at compile or runtime.