Error when De-locating Matrix - double free or corruption C

Asked

Viewed 335 times

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)

  • How is the rest of the program? You can create a minimum executable code that shows the error you indicate ?

3 answers

0

First, the allocation of matrix lines cop is possibly incorrect. The correct is:

for (int k = 0; k < x; k++)
{
    cop[k] = malloc(y * sizeof(long int));
}

In your code, you allocated relative to the size of sizeof(long int*), which depends on the size of the machine pointer. In a 32-bit operating system (OS), this size will be 4 bytes, already in a 64-bit OS, this size will be 8 bytes. If I’m not mistaken, the size of long int is 8 bytes in both cases (except on very exotic architectures).

A good way to find the source of these problems is to compile the program with debugging flags (in gcc, -g) and use Valgrind.

0

If you want a matrix in which cop->cop[0]->cop[0][0] == dado, where the dado is a long int, we would have to the first element:

long int** cop;
cop = (long int**) malloc(x * (long int*));
cop[0] = (long int*) malloc(y * (long int));
cop[0][0] = dado;

If you want a matrix in which *cop->cop[0]->cop[0][0] == matriz_original[0][0], where matriz_original stores the data directly and matriz_original[0][0] is a long int, we would have to the first element:

long int*** cop;
cop = (long int***) malloc(x * (long int**));
cop[0] = (long int**) malloc(y * (long int*));
cop[0][0] = &matriz_original[0][0]; //matriz_b armazena os dados diretamente, não ponteiros

I imagine that the second option would make sense if each element of the original matrix was too large, so you would rotate a pointer matrix that points to the original matrix. However, in case it is understood that the type of each data unit is only one long int and not a pointer to another larger structure. Maybe you want to do what is described in the first option.

0

I’ve written a little program to simulate the matrix you’re trying to make... When reading and writing in the matrix I had no errors. I also managed to run the free command without problems. Take a look at the code I wrote, run it through your staff and maybe you can find the problem.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int x = 10;
    long int **cop = (long int **) malloc (x * sizeof(long int *));

    printf("Alocando... \n");
    for (int k = 0; k < x; ++k) {
        cop[k] = (long int*) malloc (x * sizeof(long int));
    }

    printf("Atribuindo... \n");
    for (int i = 0; i < x; ++i) {
        for (int j = 0; j < x; ++j) {
            cop[i][j] = i;
        }
    }

    printf("Imprimindo...\n\n");
    for (int l = 0; l < x; ++l) {
        for (int k = 0; k < x; ++k) {
            printf("k[%d][%d]: %d\n", l, k, cop[l][k]);
        }
    }
    printf("\n\n");

    for (int k = 0; k < x; ++k) {
        free(cop[k]);
    }
    free(cop);

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.