Error when deallocating array-double free or corruption C

Hello, I'm making a program to rotate an array, which after allocating performs the rotation function, and then deallocates the data. The problem is when trying to dislodge, it is 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 the case, the cop Matrix), x and y is the size of the Matrix necessary for the image to be rotated. Theta = 90º as example

Author: Diego Ferreira, 2019-05-04

3 answers

First, the array row allocation 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 have 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 in very exotic architectures).

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

 0
Author: MrParrot, 2019-05-14 20:27:00

I wrote a small program to simulate the array you are wanting to make... When reading and writing to the array I had no errors. I also managed to run the free command without problems. Take a look at the code I wrote, run on your machine 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;
}
 0
Author: wensiso, 2019-05-14 22:15:28

If you want an array in which cop->cop[0]->cop[0][0] == dado, where dado is a long int, we would have for 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 an array 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 for 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 array was too large, then an array of pointers pointing to the original array would be rotated. However, in the case is it is implied that the type of each data unit is only a long int and not a pointer to another larger structure. Perhaps you are wanting to do the described in the first option.

 0
Author: alandplm, 2019-05-14 22:56:18