3
I created a Matrix class, to manipulate a matrix. In the class statement I have the following members (publicos):
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 start 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 matrix indices are arranged along 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 matrix:
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, add a column to the matrix:
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 executing this function (main) in the putColumn method of the Matrix I get the following error: Access Violation writing Location 0xcdcdcd. I’d appreciate it if someone could help me, thank you!
You are programming middle in C and middle in C++. You should choose a style and keep it. As you are using C++ you should not use
malloc
,memcpy
, raw pointers and things like that. Even in C it shouldn’t do cast in themalloc
. http://stackoverflow.com/q/605845/221800. I have trouble finding generic errors in full-pointer codes just by looking at them. It would be interesting to give better information or better, post something that we can execute and see what is happening: http://answall.com/help/mcve– Maniero
Off you are mixing types. Your "Matrix" is created from
unsigned char
but when you insert columns you useint
. On a 32bit architectureunsigned char
will have 1 byte and theint
4 bytes...– Luiz Vieira
@Luiz Vieira Even when inserting uchar columns it still gives error, so I deduced that the problem did not come from there.
– MarcoAF
Well, I don’t know what the main problem is, but inserting columns of integers into something allocated like char will give error as well. :)
– Luiz Vieira
Another thing that is "weird": you allocate the new column by multiplying by
nlinhas
(which, moreover, should not benRows
?), but thememcpy
uses (correctly)nCols
. Looks like you should allocate the new column fromnCols
.– Luiz Vieira
@bigown I know I’m mixing languages, but it’s intentional. I think the malloc cast is mandatory for c++. When debugging the error appears in memcpy. I do not see what else I can edit, because that is what is essential, and I think that there is no lack of information in the code posted. I’m grinding my head a little bit with this, but I can’t solve the problem. Thanks for your help :)
– MarcoAF
@Luiz Vieira nRows is a member of my class, this part of the code corresponds to the main function and does not belong to the Matrix class. I used them just to know that they were the number of lines :) Imagine that after that and before Matrix.putColumn(col,0); I called the class constructor passing as arguments nLinhas and nColunas. : ) Thank you
– MarcoAF
But, Marco, if
nlinhas
has a different value thannCols
, will give even error. Because out you allocate*col
fromnlinhas*sizeof(int)
and inside you copy withmemcpy(p,Col,nCols)
!– Luiz Vieira
@Luiz Vieira PS: I already changed the input of the columns to play and the error remains... When debugging the code gives error in memcpy. Then you’d have to have something like: memcpy(p,Col,nRows)?
– MarcoAF