堆腐败。 C

时间:2012-01-09 20:14:20

标签: c heap heap-corruption

我有一堆腐败,无法找到原因。拜托,你能帮忙吗? 我有一些代码片段,我想到错误的位置。 这里生成了堆损坏(参见下面的注释):

 free(rowPermutation);
 fclose(wFile);

所以,内存分配就在这里:

static int N = 2,**orderOfRows, *columnsPermutation,*tmpRowPermutation,*resultPermutation,
    *u,*v,**sourceMatrix,**patternMatrix,**auxMatrix1,*incidence,*perm;

 static FILE *wFile,*file,*patternFile;

 void allocate2dMemory() {

    int i = 0;

    sourceMatrix = (int**) malloc(N * sizeof(int *));
    auxMatrix1= (int**) malloc(N * sizeof(int *));
    orderOfRows = (int**) malloc(N * sizeof(int*));
    patternMatrix = (int**) malloc(N * sizeof(int*));
    incidence = (int*) malloc(N * sizeof(int));
    columnsPermutation = (int*) malloc(N * sizeof(int));
    tmpRowPermutation = (int*) malloc(N * sizeof(int));
    resultPermutation = (int*) malloc(N * sizeof(int));
    perm = (int*)malloc(N * sizeof(int));

    u = (int*) malloc(N * sizeof(int));
    v = (int*) malloc(N * sizeof(int));

    if ((sourceMatrix == NULL) || (auxMatrix1 == NULL) || (incidence == NULL) || (orderOfRows == NULL) || 
            (columnsPermutation == NULL) || (tmpRowPermutation == NULL) || (u == NULL) || (v == NULL) || (resultPermutation == NULL)) {
            fprintf(stderr, "out of memory\n");
            exit(2);
    }


    for (i = 0; i < N; i++) {
            sourceMatrix[i] = (int*) malloc(N * sizeof(int));
            auxMatrix1[i] = (int*) malloc(N * sizeof(int));
            patternMatrix[i] = (int*) malloc(N * sizeof(int));
            incidence[i] = 0;
            if ((sourceMatrix[i] == NULL) || (auxMatrix1[i] == NULL) || (patternMatrix[i] == NULL) ) {
                            fprintf(stderr, "out of memory\n");
                            exit(2);
            }
    }

}

打开文件:

 void openFile(char* filename) {
    if ((file = fopen(filename, "r")) == NULL) {
            perror("Open error");
            printf("\nPress any key for exit\n\n");
            getch();
            exit(1);
    }


    if ((patternFile = fopen("pattern.dat", "r")) == NULL) {
            perror("Open error");
            printf("\nPress any key for exit\n\n");
            getch();
            exit(1);
    }

    if ((wFile = fopen("out.txt", "w")) == NULL) {
            perror("Open error");
            printf("\nPress any key for exit\n\n");
            getch();
            exit(1);
    }

然后我关闭其中一些(“wFile”是用于写作的文件):

  fclose(file);
  fclose(patternFile);

更改行顺序:

  void changeRowOrder(int *computation,int **matr) {

    fprintf(wFile,"Make row permutation\n");

    int i,j;

    for (i = 0; i < N; ++i) {
            orderOfRows[computation[i]] = matr[i];
    }
    fputs("\n",wFile);
}

更改列的顺序:

   int **destMatrix = (int**) malloc(N * sizeof(int *));

    if ((destMatrix == NULL)) {
            fprintf(stderr, "out of memory\n");
            exit(2);
    }

    for (i = 0; i < N; i++) {
            destMatrix[i] = (int*) malloc(N * sizeof(int));

            if (destMatrix[i] == NULL) {
                    fprintf(stderr, "out of memory\n");
                    exit(2);
            }
    }

    for(i = 0; i < N; ++i) {
            // save permutation
            resultPermutation[perm[i]] = i;
            for(j = 0; j < N; ++j) {
                    destMatrix[i][j] = orderOfRows[i][perm[j]];
            }
    }

    fprintf(wFile,"Now result permutation is: \n");
    printArray(resultPermutation);

    for(i = 0; i < N; ++i) {
            free(sourceMatrix[i]);
    }
    free(sourceMatrix);

    sourceMatrix = destMatrix;

我需要静态指针才能在我的所有程序中看到它们。 这里找到了另一个可能存在错误的代码:

以下代码位于程序的开头。

    int res,i,j;
    char c[25],f[25],g;

    int *rowPermutation = (int*)malloc(N*sizeof(int));

    openFile("inb.dat");
    fscanf(file,"%s %s %d %d",&c,&f,&N,&i);
    allocate2dMemory();
    getMaxtrix();
    // and so on ...
     free(rowPermutation);
    fclose(wFile);

我没有在我的程序的其他地方分配内存。 我注意到“columnsPermutation”数组中的内存已损坏。  首先,我复制一些元素,然后元素开始改变。当我使用STL容器来修复堆损坏时,我已经注意到了它(只是为了知道数组的不同之处)。

拜托,你能找到错误吗?在我看来,我正确地分配了内存。

enter image description here

2 个答案:

答案 0 :(得分:2)

Nmalloc()拨打rowPermutation时,N的价值是多少?因为fscanf()使用rowPermutationmalloc()元素分配内存后,我发现N的值为N。如果int *rowPermutation = (int*)malloc(N*sizeof(int)); // What is the value of N when executing the above code? openFile("inb.dat"); fscanf(file,"%s %s %d %d",&c,&f,&N,&i); // N is obtained after malloc'ing memory to rowPermutation 未正确初始化,则可能包含垃圾值。

{{1}}

OTOH,最好使用valgrind之类的工具来检查内存泄漏问题。

答案 1 :(得分:0)

即使是最好的程序员,与堆损坏相关的错误也很常见。我建议从valgrind.org下载valgrind,然后快速了解该程序附带的内容,而不是一行一行。 Valgrdind是免费的,并且带有最丰富的Linux版本。

商业替代品是来自parasoft.com的insure ++。

应始终根据内存问题检查生产代码,此后由于这个原因,安装工具是一个很好的机会。

关于你的计划:

if ((sourceMatrix[i] == NULL) || (auxMatrix1[i] == NULL) || (patternMatrix[i] == NULL) ) {

如果其中一些指针为NULL,则表示您正在退出。在调用exit(2)之前,您需要释放已经分配的指针。每次发生异常(指针== NULL)时,您还需要注意(自由)在异常之前分配的指针。