我如何释放结构?

时间:2016-10-27 02:45:07

标签: c matrix struct malloc free

我有一个主文件,它接收一个文件并将文件中的数字添加到矩阵中。我有一切工作,它打印出正确的答案,但在valgrind我得到泄漏,我肯定知道它,因为我没有在循环释放MatC:否则if(numOfMatrices> 2)。前面带箭头的线条是需要释放的线条。

这是主要功能:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mmult.h"

int main(int argc, char* argv[]){
    (void) argc;
    FILE* fp = fopen(argv[0], "r"); 
    char *firstLine = NULL;
    char *ptr;
    if (!fp){
        fprintf(stderr, "Can't Open File");
        return EXIT_FAILURE;
    } else{ // Gets the number of matrices
        char *buf = NULL;
        size_t len = 100;
        while (getline(&buf, &len, stdin)){
            char *pch;
            pch = strtok(buf, " \n");
            firstLine = buf;
            (void) pch;
            break;
        }
    }

    int numOfMatrices = strtod(firstLine, &ptr);
    free(firstLine);

    // Gets the number of rows and columns of the matrix
    for (int m = 0; m < numOfMatrices; m++){
        int x, y, i, j;
        Matrices MatA;
        Matrices MatB;
        Matrices MatC;
        Matrices MatD;
        if (numOfMatrices == 1){
            MatA.Matrix = mread(stdin, &x, &y);
            MatA.row = x;
            MatA.col = y;

            printf("%d %d\n", MatA.row, MatA.col);
            mwrite(stdout, MatA.row, MatA.col, MatA.Matrix);
            xfree(MatA.row, MatA.col, MatA.Matrix);
        } else if (numOfMatrices >= 2){
            if (numOfMatrices == 2){
                if (m == 0){
                    MatA.Matrix = mread(stdin, &x, &y);
                    MatA.row = x;
                    MatA.col = y;

                    MatB.Matrix = mread(stdin, &i, &j);
                    MatB.row = i;
                    MatB.col = j;

                    MatC.Matrix = mmult(MatA.row, MatA.col, MatA.Matrix, MatB.row, MatB.col, MatB.Matrix);
                    MatC.row = MatA.row;
                    MatC.col = MatB.col;

                    xfree(MatA.row, MatA.col, MatA.Matrix);
                    xfree(MatB.row, MatB.col, MatB.Matrix);
                    printf("%d %d\n", MatC.row, MatC.col);
                    mwrite(stdout, MatC.row, MatC.col, MatC.Matrix);
                    xfree(MatC.row, MatC.col, MatC.Matrix);
                }
             } else if (numOfMatrices > 2){
                if (m == 0){
                    MatA.Matrix = mread(stdin, &x, &y);
                    MatA.row = x;
                    MatA.col = y;

                    MatB.Matrix = mread(stdin, &i, &j);
                    MatB.row = i;
                    MatB.col = j;

                ->  MatC.Matrix = mmult(MatA.row, MatA.col, MatA.Matrix, MatB.row, MatB.col, MatB.Matrix);
                    MatC.row = MatA.row;
                    MatC.col = MatB.col;

                    xfree(MatA.row, MatA.col, MatA.Matrix);
                    xfree(MatB.row, MatB.col, MatB.Matrix);

                    for (int x = 2; x < numOfMatrices; x++){
                        MatD.Matrix = mread(stdin, &i, &j);
                        MatD.row = i;
                        MatD.col = j;

                    ->  MatC.Matrix = mmult(MatC.row, MatC.col, MatC.Matrix, MatD.row, MatD.col, MatD.Matrix);

                        xfree(MatD.row, MatD.col, MatD.Matrix);
                        if (x == (numOfMatrices-1)){
                            printf("%d %d\n", MatC.row, MatC.col);
                            mwrite(stdout, MatC.row, MatC.col, MatC.Matrix);
                            xfree(MatC.row, MatC.col, MatC.Matrix);
                        }

                    }
                } 
            }
        }
    }
    fclose(fp);
}

另外,这是我的xfree函数,它是一个矩阵:

// Free all memory allocated for A 
void xfree(int r, int c, double **A){
    (void) c;
    for (int y = 0; y < r; y++){
        free(A[y]);
    }
    free(A);
}

我还为矩阵创建了一个结构:

typedef struct Matrices{
        double **Matrix;    // The matrix held in this node
        int row;            // Number of rows in the Matrix
        int col;            // Number of Columns in the Matrix
    } Matrices;

Valgrind输出:

ShaolinGOD@comp:~/Desktop/Project2$ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./mmult < OneMatrix.txt
==7984== Memcheck, a memory error detector
==7984== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==7984== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==7984== Command: ./mmult
==7984== 
2 2
   52.00    52.00 
   80.00    80.00 
==7984== 
==7984== HEAP SUMMARY:
==7984==     in use at exit: 144 bytes in 9 blocks
==7984==   total heap usage: 30 allocs, 21 frees, 133,624 bytes allocated
==7984== 
==7984== 32 bytes in 2 blocks are indirectly lost in loss record 1 of 4
==7984==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7984==    by 0x400D11: xalloc (mmult.c:83)
==7984==    by 0x400BF7: mmult (mmult.c:65)
==7984==    by 0x401111: main (mmult-driver.c:86)
==7984== 
==7984== 48 (16 direct, 32 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 4
==7984==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7984==    by 0x400CDE: xalloc (mmult.c:81)
==7984==    by 0x400BF7: mmult (mmult.c:65)
==7984==    by 0x401111: main (mmult-driver.c:86)
==7984== 
==7984== 64 bytes in 4 blocks are indirectly lost in loss record 3 of 4
==7984==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7984==    by 0x400D11: xalloc (mmult.c:83)
==7984==    by 0x400BF7: mmult (mmult.c:65)
==7984==    by 0x4011A4: main (mmult-driver.c:98)
==7984== 
==7984== 96 (32 direct, 64 indirect) bytes in 2 blocks are definitely lost in loss record 4 of 4
==7984==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7984==    by 0x400CDE: xalloc (mmult.c:81)
==7984==    by 0x400BF7: mmult (mmult.c:65)
==7984==    by 0x4011A4: main (mmult-driver.c:98)
==7984== 
==7984== LEAK SUMMARY:
==7984==    definitely lost: 48 bytes in 3 blocks
==7984==    indirectly lost: 96 bytes in 6 blocks
==7984==      possibly lost: 0 bytes in 0 blocks
==7984==    still reachable: 0 bytes in 0 blocks
==7984==         suppressed: 0 bytes in 0 blocks
==7984== 
==7984== For counts of detected and suppressed errors, rerun with: -v
==7984== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
ShaolinGOD@comp:~/Desktop/Project2$ 

1 个答案:

答案 0 :(得分:1)

xfree功能没问题。问题是xfree

的所有分配都没有调用MatC.Matrix

您似乎正在尝试转换MatC.Matrix。为此,您可以分配一个临时矩阵,将旧矩阵复制到临时矩阵,释放旧矩阵,然后将旧矩阵指针设置为临时矩阵。

示例,请更改以下部分:

MatC.Matrix = mmult(MatA.row, MatA.col, MatA.Matrix, MatB.row, MatB.col, MatB.Matrix);
MatC.row = MatA.row;
MatC.col = MatB.col;

xfree(MatA.row, MatA.col, MatA.Matrix);
xfree(MatB.row, MatB.col, MatB.Matrix);

for (int x = 2; x < numOfMatrices; x++) 
{
    MatD.Matrix = mread(stdin, &i, &j);
    MatD.row = i;
    MatD.col = j;

    MatC.Matrix = mmult(MatC.row, MatC.col, MatC.Matrix, MatD.row, MatD.col, MatD.Matrix);
    xfree(MatD.row, MatD.col, MatD.Matrix);

    if (x == (numOfMatrices - 1)) 
    {
        printf("%d %d\n", MatC.row, MatC.col);
        mwrite(stdout, MatC.row, MatC.col, MatC.Matrix);
        xfree(MatC.row, MatC.col, MatC.Matrix);
    }
}

对此:

MatC.Matrix = mmult(MatA.row, MatA.col, MatA.Matrix, MatB.row, MatB.col, MatB.Matrix);
MatC.row = MatA.row;
MatC.col = MatB.col;

xfree(MatA.row, MatA.col, MatA.Matrix);
xfree(MatB.row, MatB.col, MatB.Matrix);

for (int x = 2; x < numOfMatrices; x++) 
{
    MatD.Matrix = mread(stdin, &i, &j);
    MatD.row = i;
    MatD.col = j;

    Matrices temp;
    temp.Matrix = mmult(MatC.row, MatC.col, MatC.Matrix, MatD.row, MatD.col, MatD.Matrix);
    xfree(MatD.row, MatD.col, MatD.Matrix);

    //free MatC.Matrix
    xfree(MatC.row, MatC.col, MatC.Matrix);

    //copy pointers from temp to MatC.Matrix
    MatC.Matrix = temp.Matrix;
    for (int k = 0; k < temp.row; k++) 
        MatC.Matrix[k] = temp.Matrix[k];
    MatC.row = temp.row;//<== added (EDIT ***********)
    MatC.col = temp.col;//<== added (EDIT ***********)

    if (x == (numOfMatrices - 1)) 
    {
        printf("%d %d\n", MatC.row, MatC.col);
        mwrite(stdout, MatC.row, MatC.col, MatC.Matrix);
    }
}

//add xfree here:
xfree(MatC.row, MatC.col, MatC.Matrix);

现在分配数量和xfree应匹配。