c - 我如何释放一个结构?

标签 c matrix struct malloc free

我有一个 main 函数,它接收一个文件并将文件中的数字添加到矩阵中。我一切正常,它打印出正确的答案,但在 valgrind 中我遇到了泄漏,我肯定知道它,因为我没有在循环中释放 MatC: else 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 是一个矩阵:

// 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$ 

最佳答案

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 应该匹配。

关于c - 我如何释放一个结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40275274/

相关文章:

c++ - 将单声道编译为静态库

C 字符串和指针

arrays - 在 matlab 中创建 3D 矩阵

json - 可以在不使用结构的情况下在 Golang 的 DynamoDB 中运行 PutItem 吗?

c - C 中的位操作 - 内置函数

跨平台小部件工具包

c++ - 使用 Eigen 和 C++ 做大量矩阵乘积的列和

r - 如何使用 R 跳过我要导入的数据文件中的注释行

go - 如何使用变量访问结构?

apache-spark - Spark Struct 结构域名称在 UDF 中发生变化