I created a Matrix class, to manipulate an array. In the class declaration I have the following members (public):
class Matrix{
public:
unsigned char **mat; //ponteiro para ponteiro de uchar
int nRows; //numero de linhas
int nCols; //numero de colunas da matriz
Matrix(int nRows, int nCols); //construtor
void putColumn(int *Col, int j); //metodo para adicionar colunas à matriz
}
In the constructor I initialize nRows and nCols and allocate memory to mat.
Matrix::Matrix(int rows, int cols)
//Construtor da classe que recebe número de linhas e número de colunas.
{
nRows = rows;
nCols = cols;
mat = (unsigned char**)malloc(nRows*nCols*sizeof(unsigned char));
}
The indexes of the array are arranged along the memory sequentially, as if we had an array, and to access them would be something like:
unsigned char * p;
p = mat[i] + j*nRows;//onde i = linha e j =coluna
Then I have a method to add columns to the array:
void Matrix::putColumn(int *Col, int j)
{
unsigned char *p;
p = *mat + j*nRows;//j é a coluna que pretendo adicionar
//ou seja, coluna 1, 2, 3, 4 até completar a matriz
memcpy(p,Col,nCols);
}
In the main function after creating a Matrix object, I add a column to the array:
unsigned char *col;
col = (unsigned char*) malloc(nlinhas*sizeof(unsigned char));
for ( int i = 1; i <= nlinhas; i++){
col[i] = i;
}
matrix.putColumn(col,0);
The problem here is that when I run this function (main) on the Matrix putColumn method I get the following error: Access violation writing location 0xcdcdcdcd. Thanks if anyone could help me, thank you!