0

I get the following error when running a c program: * Error in `./final': double free or corruption (!prev): 0x0000000000c2f010 *

I believe this is due to free() being called at the end of the program, but I can't figure out where the malloc'd memory is being freed prior to this. Here is the code:

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

static int DIM = 1000; // Golbal variable for Matrices Dimentions
int isPrime(unsigned int number);
int countPrimes(int **matrix);

int main() {

    time_t tic = clock();
    int **matrixA;//
    int **matrixB;
    int **mult;
    int k = 0, sum=0;
    time_t t;
    int i =0,j;

    // Allocate dynamic memory for Matrices A,B && mult first (rows) dimension
    matrixA = (int **) malloc (DIM * sizeof (int *));
    matrixB = (int **) malloc (DIM * sizeof (int *));
    mult = (int **) malloc (DIM * sizeof (int *));
    // Allocate dynamic memory for Matrices A,B && mult second (rows) dimension, && Initialize elements to "0" For Matrices B && Mult
    for (k = 0; k < DIM; k++) {
        matrixA[k] = (int *) calloc (DIM, sizeof (int *));
        matrixB[k] = (int *) calloc (DIM, sizeof (int *));
        mult[k] = (int *) calloc (DIM, sizeof (int *));
    }

    // Generate random numbers for matrix A elements
    for (i = 0; i < DIM; i++) 
    {
        for (j = 0; j < DIM; j++) 
        {
            matrixA[i][j] = (rand() % 100)+1 ;
            printf("%d  ",matrixA[i][j]);
        }
    }

    // Do the matrix multiplication
    for (i = 0; i < DIM; i++) {
        for (j = 0; j < DIM; j++) {
            for (k = 0; k < j; k++) {
                sum += matrixA[i][k] * matrixB[k][j];
            }
            mult[i][j] = sum;
        sum = 0;
        }
    }

    int total = countPrimes(matrixA);
    printf("\n\nnumber of primes in a : %d\n\n", total);

  // Delete the dynamic memory of Matrix A
    for (i = 0; i < DIM; i++) {          // free first matrix
        free(*(matrixA+i));
    }
    free(matrixA);

    // Delete the dynamic memory of Matrix B
    for (i = 0; i < DIM; i++) {          // free first matrix
        free(*(matrixB+i));
    }
    free(matrixA);

    // Delete the dynamic memory of Matrix B
    for (i = 0; i < DIM; i++) {          // free first matrix
        free(*(matrixB+i));
    }
    free(matrixA);


    // Compute SpeedUp
    time_t toc = clock();
    double time = (double) (toc - tic) / CLOCKS_PER_SEC;
    printf("The amount of time the OS has spent running your process is : %lf seconds\n\n\n", time);

    return 0;
}

// Check elements of Matrix A if its a Prime number or not
int isPrime(unsigned int number) { // Psitive numbers could be Prime numbers only
    if (number <= 1) return 0; // zero and one are not prime
    unsigned int i;
    for (i=2; i*i<=number; i++) {
        if (number % i == 0) return 0;
    }
    return 1;
    }

// Count number of prime numbers in Matrix A
int countPrimes(int **matrix) {
    int row, col;
    int flag = 0;
    int total = 0;
    for (row = 0; row < DIM; row++) {
        for (col = 0; col < DIM; col++) {
            flag = isPrime(matrix[row][col]);
            if(flag == 1)
                total++;
        }
    }
    return total;
}
  • Welcome to Stack Overflow. Please have a look at [Tour](https://stackoverflow.com/tour) and [Asking](https://stackoverflow.com/help/asking). – Shiro Jun 03 '17 at 23:12
  • *** Error in `./final': double free or corruption (!prev): 0x0000000000c2f010 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f3bc0d747e5] /lib/x86_64-linux-gnu/libc.so.6(+0x7fe0a)[0x7f3bc0d7ce0a] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f3bc0d8098c] ./final[0x4009db] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3bc0d1d830] ./final[0x4005b9] ======= Memory map: ======== 00400000-00401000 r-xp 00000000 08:0b 1966527 /home/suhaib/Desktop/final 00600000-00601000 r--p 00000000 08:0b 1966527 – Suhaib Ibrahim Jun 03 '17 at 23:14
  • That wall a junk belongs in your question, not in a comment. And fyi, your inner-dimension `calloc (DIM, sizeof (int *));` allocations are all using the wrong `sizeof`. They should use `sizeof(int)` – WhozCraig Jun 03 '17 at 23:15
  • 'I believe this is due to free() being called at the end of the program' believe? What happens when you commented out that last free? – ThingyWotsit Jun 03 '17 at 23:15
  • Parts of `// Delete the dynamic memory of Matrix ?` You wrong copy&paste – BLUEPIXY Jun 03 '17 at 23:15
  • 2
    I.e. `free(matrixA);` is done three times. oh copy/paste! ye are a cruel an heartless wench. (your `B` matrix is also inner-free'd twice, btw, also a copy/paste bug). – WhozCraig Jun 03 '17 at 23:16
  • thanks alot that was the problem – Suhaib Ibrahim Jun 04 '17 at 00:29
  • Possible duplicate of [How to track down a double free or corruption error in C++ with gdb](https://stackoverflow.com/questions/2902064/how-to-track-down-a-double-free-or-corruption-error-in-c-with-gdb) – Raedwald Dec 06 '18 at 09:05

1 Answers1

1

Your error is here:

  // Delete the dynamic memory of Matrix A
    for (i = 0; i < DIM; i++) {          // free first matrix
        free(*(matrixA+i));
    }
    free(matrixA);

    // Delete the dynamic memory of Matrix B
    for (i = 0; i < DIM; i++) {          // free first matrix
        free(*(matrixB+i));
    }
    free(matrixA);

    // Delete the dynamic memory of Matrix B
    for (i = 0; i < DIM; i++) {          // free first matrix
        free(*(matrixB+i));
    }
    free(matrixA);

Notice that you free(matrixA) three times, even though you intend to free matrixB and matrixC. You also mistakenly use free(*(matrixB+i)); in two different loops.

David Hoelzer
  • 15,862
  • 4
  • 48
  • 67