Мы должны сделать матричные операции для школы, и у меня возникает утечка памяти где-то при освобождении матриц. Я искал утечку в течение нескольких часов и до сих пор не могу ее найти. Проблема, вероятно, где-то между освобождающими матрицами во время вычисления.
stdin:
1 2 (количество строк и столбцов)
6 4 (значения матрицы)
+
1 2
-6 7
+
1 2
-6 -4
Valgrind:
==1480== HEAP SUMMARY:
==1480== in use at exit: 56 bytes in 3 blocks
==1480== total heap usage: 17 allocs, 14 frees, 8,472 bytes allocated
==1480==
==1480== 56 (32 direct, 24 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==1480== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1480== by 0x400812: make_matrix (main.c:42)
==1480== by 0x400AFA: calculate_operation (main.c:97)
==1480== by 0x4010BE: main (main.c:193)
==1480==
==1480== LEAK SUMMARY:
==1480== definitely lost: 32 bytes in 1 blocks
==1480== indirectly lost: 24 bytes in 2 blocks
==1480== possibly lost: 0 bytes in 0 blocks
==1480== still reachable: 0 bytes in 0 blocks
==1480== suppressed: 0 bytes in 0 blocks
Мой код (игнорировать сообщения stderr, они на чешском языке)
- включены только важные части
#include <stdio.h>
#include <stdlib.h>
#define POINTERSIZE 8
#define MAXMATRIXES 100
typedef struct
{
int row_count, column_count;
int **values;
} Matrix;
void free_matrix(Matrix *matrix)
{
for (int i = 0; i < matrix->row_count; i++)
{
free(matrix->values[i]);
}
free(matrix->values);
free(matrix);
}
Matrix *make_matrix(int row_count, int column_count)
{
Matrix *matrix = malloc(column_count * row_count * 2 * POINTERSIZE); ***(PROBLEM HERE)***
matrix->row_count = row_count;
matrix->column_count = column_count;
matrix->values = malloc(column_count * row_count * POINTERSIZE);
for (int i = 0; i < row_count; i++)
{
matrix->values[i] = malloc(column_count * sizeof(int));
}
return matrix;
}
Matrix *calculate_operation(Matrix *matrix1, Matrix *matrix2, char operator)
{
if (is_operation_possible(matrix1, matrix2, operator) != 1)
{
fprintf(stderr, "Error: Chybny vstup!\n");
free_matrix(matrix1);
free_matrix(matrix2);
exit(100);
}
Matrix *result;
switch (operator)
{
case '+':
result = make_matrix(matrix1->row_count, matrix1->column_count); ***(PROBLEM HERE)***
for (int i = 0; i < result->row_count; i++)
{
for (int j = 0; j < result->column_count; j++)
{
result->values[i][j] = matrix1->values[i][j] + matrix2->values[i][j];
}
}
break;
}
Matrix *load_matrix()
{
int row_count, column_count, input_check;
input_check = scanf("%d %d", &row_count, &column_count);
if (input_check != 2 || row_count <= 0 || column_count <= 0)
{
fprintf(stderr, "Error: Chybny vstup!\n");
exit(100);
}
Matrix *matrix = make_matrix(row_count, column_count);
fill_matrix(matrix, row_count, column_count);
return matrix;
}
void adjust_matrixes(Matrix *matrixes[], char operators[])
{
for (int i = 0; i < MAXMATRIXES; i++)
{
while (operators[i] == '*' && operators[i + 1])
{
matrixes[i] = calculate_operation(matrixes[i], matrixes[i + 1], '*');
free_matrix(matrixes[i + 1]);
operators[i] = operators[i + 1];
for (int j = i + 1; j < MAXMATRIXES - 1; j++)
{
matrixes[j] = matrixes[j + 1];
operators[j] = operators[j + 1];
}
}
}
}
int main(int argc, char *argv[])
{
char operators[MAXMATRIXES];
for (int i = 0; i < MAXMATRIXES; i++)
{
operators[i] = 0;
}
Matrix *matrixes[MAXMATRIXES];
for (int i = 0; i < MAXMATRIXES; i++)
{
matrixes[i] = load_matrix();
scanf(" %c", &operators[i]);
if (operators[i] != '+' && operators[i] != '-' && operators[i] != '*')
{
operators[i] = 0;
break;
}
}
adjust_matrixes(matrixes, operators);
int index = 0;
Matrix *result = matrixes[index];
while (operators[index] != 0)
{
result = calculate_operation(result, matrixes[index + 1], operators[index]); ***(PROBLEM HERE)***
free_matrix(matrixes[index]);
index++;
}
free_matrix(matrixes[index]);
print_matrix(result);
free_matrix(result);
return 0;
}