1

I m having some troubles with my code, the thing is I don't really get SEG FAULT and I can run it ok and also get good results, but when I use valgrind I get some memory leak-errors like this:

==2265== Invalid read of size 4
==2265==    at 0x109A3B: move (in /home/cosmi/Desktop/315CA_CojocaruCosmin_Tema3/snowfight)
==2265==    by 0x10B419: main (in /home/cosmi/Desktop/315CA_CojocaruCosmin_Tema3/snowfight)

==2265==  Address 0x55ccb88 is 8 bytes inside a block of size 20 free'd

==2265==    at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==2265==    by 0x10A0F6: meltdown (in /home/cosmi/Desktop/315CA_CojocaruCosmin_Tema3/snowfight)

==2265==    by 0x10B4A9: main (in /home/cosmi/Desktop/315CA_CojocaruCosmin_Tema3/snowfight)

==2265==  Block was alloc'd at
==2265==    at 0x4C31B25: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2265==    by 0x10AFE9: main (in /home/cosmi/Desktop/315CA_CojocaruCosmin_Tema3/snowfight)

The problem is that I pass two matrices alloced(with calloc) in main() to a function named meltdown(). In this function what I should is resizing those two, taking the first and last columns and lines away from them. So from a 5x5 I should get a 3x3 matrix. I don't know how I could do that with realloc so I used another two matrix to copy elemnts into and then make initial matrix pointers point to those two..

int main() {
    int **a = calloc((2 * R + 1) , sizeof(int*));

    int **b = calloc((2 * R + 1) , sizeof(int*));

    for (i = 0; i < (2 * R + 1); i++) {
        a[i] = calloc((2 * R + 1) , sizeof(int));
        b[i] = calloc((2 * R + 1) , sizeof(int));
    }
}

void meltdown(int **a, int **b, int R, ..) {
    int n = 2 * R + 1;
    int **c = malloc((n - 2) * sizeof(int*)); 
    for(i = 0; i < n - 2; i++) 
        c[i] = malloc((n-2)*sizeof(int));
    l = 0;
    for(i = 1; i < n - 1; i++) {
        k = 0;
        for(j = 1; j < n - 1; j++) {
            c[l][k] = a[i][j];
            k++;
        }
        l++;
    }
    for(i = 0; i < n; i++) 
        free(a[i]);
    free(a);
    a = c;
   //same goes for b
}

I m actually frustrated because my code really seems to work fine on the actual problem and solves it well but I get 0 points because of this memory leak problem.

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

3

a = c assigns the local variable inside meltdown(), it doesn't affect the caller's variable. If main() tries to use its a pointer it will access memory that was freed by free(a);, causing undefined behavior.

If meltdown() should update the caller's variable, you need to pass a pointer to the variable, and dereference this variable in meltdown().

void meltdown(int ***aptr, int ***bptr, int R) {
    int **a = *aptr;
    int **b = *bptr;
    int n = 2 * R + 1;
    int **c = malloc((n - 2) * sizeof(int*)); 
    for(i = 0; i < n - 2; i++) 
        c[i] = malloc((n-2)*sizeof(int));
    l = 0;
    for(i = 1; i < n - 1; i++) {
        k = 0;
        for(j = 1; j < n - 1; j++) {
            c[l][k] = a[i][j];
            k++;
        }
        l++;
    }
    for(i = 0; i < n; i++) 
        free(a[i]);
    free(a);
    *aptr = c;
   //same goes for b
}

When you call the function you have to take the address:

meltdown(&a, &b, R);
Barmar
  • 741,623
  • 53
  • 500
  • 612