双重自由或腐败(!prev) [英] double free or corruption (!prev)

查看:94
本文介绍了双重自由或腐败(!prev)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行c程序时收到以下错误: *`./final'中的错误:双重释放或损坏(!prev):0x0000000000c2f010 *

我认为这是由于free()在末尾被调用 程序,但我不知道malloc的内存在哪里 在此之前释放.这是代码:

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;
}

推荐答案

您的错误在这里:

  // 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);

即使您打算释放matrixB和matrixC,也请注意您free(matrixA)三次.您还会错误地在两个不同的循环中使用free(*(matrixB+i));.

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.

这篇关于双重自由或腐败(!prev)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆