0
Hello, I’m running a program to rotate a matrix, which after allocating executes the rotation function, and then displaces the data. The problem is when I try to dislocate, it’s returning me double free or corruption
cop = malloc(x * sizeof(long int*));
for (int k = 0; k < x; k++)
{
cop[k] = malloc(y * sizeof(long int*));
}
transf(mat,cop,lin,col,theta,variavel);
for (int i = 0; i < x; i++)
{
free(cop[i]);
}
free(cop);
The function used is this:
void transf(long int **mat, long int **cop, long int lin, long int col,long int theta, long int var)
{
long int valor;
//x*sin(theta*PI/180) + y*cos(theta*PI/180);
//x*cos(theta*PI/180) - y*sin(theta*PI/180);
for (int i = 0; i < lin; i++)
{
for (int k = 0; k < col; k++)
{
long int a = k*cos(theta*PI/180) - i*sin(theta*PI/180);
long int b = k*sin(theta*PI/180) + i*cos(theta*PI/180);
a += var;
cop[b][a] = mat[i][k];
if (a > 0)
{
cop[b][a-1] = mat[i][k];
}
}
}
}
This function relates the Lin/col of one matrix and rotates to b/a of another (in this case, the Cop matrix), x and y is the size of the matrix needed for the image to be rotated. Theta = 90º as an example
I think this here "Cop[k] = malloc(y * sizeof(long int*));" should look like this: "Cop[k] = malloc(y * sizeof(long int));"...it doesn’t seem to me that this is the cause of the free error, yet it is something that is "wrong" (in Windows 64 bits, pointer is 64 bits but long int is 32 bits)
– zentrunix
How is the rest of the program? You can create a minimum executable code that shows the error you indicate ?
– Isac