파일을 가져 와서 파일 내의 숫자를 행렬에 추가하는 main이 있습니다. 난 모든 일을하고 올바른 답변을 인쇄하지만 valgrind 내가 누수가 확실 해요, 나는 루프에서 MatC를 해제하지 않기 때문에 확실히 : else if (numOfMatrices> 2). 그것의 앞에 화살을 가진 선은 free'd 일 필요가있는 선이다.구조체를 해제하는 방법은 무엇입니까?
는 다음의 주요 기능은 다음과 같습니다
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;
:
// 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);
}
나는 또한 행렬에 대한 구조체를 생성 : 또한
#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 기능입니다 Valgrind 출력 :
[email protected]:~/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)
[email protected]:~/Desktop/Project2$
valgrind 출력을 추가했습니다. – ShaolinGOD
귀하의 질문은 무엇입니까? 해방해야 할 것이 무엇인지 확인했다면 왜 xfree 함수를 호출 할 수 없습니까? 정확히 어떤 문제가 있는지 명확하지 않습니다. – kaylum
'while (...) {...; 단절; }'!? 왜 안 될까? if (...) {...; }'? –