我们将在学校进行矩阵运算,释放矩阵时我在某处发生内存泄漏。我已经搜索了几个小时的泄漏,但仍然找不到它。问题可能出在计算操作期间释放矩阵之间。
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 *fill_matrix(Matrix *matrix, int row_count, int column_count)
{
for (int i = 0; i < row_count; i++)
{
for (int j = 0; j < column_count; j++)
{
int input_check = scanf("%d", &matrix->values[i][j]);
if (input_check != 1)
{
fprintf(stderr, "Error: Chybny vstup!\n");
free_matrix(matrix);
exit(100);
}
}
}
return 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;
}
void print_matrix(Matrix *matrix)
{
printf("%d %d\n", matrix->row_count, matrix->column_count);
for (int i = 0; i < matrix->row_count; i++)
{
for (int j = 0; j < matrix->column_count; j++)
{
if (j == 0)
printf("%d", matrix->values[i][j]);
else
printf(" %d", matrix->values[i][j]);
}
printf("\n");
}
}
int is_operation_possible(Matrix *matrix1, Matrix *matrix2, char operator)
{
if (operator== '+' || operator== '-')
{
if (matrix1->column_count == matrix2->column_count && matrix1->row_count == matrix2->row_count)
return 1;
}
else if (operator== '*')
{
if (matrix1->column_count == matrix2->row_count)
return 1;
}
return 0;
}
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;
case '-':
result = make_matrix(matrix1->row_count, matrix1->column_count);
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;
case '*':
result = make_matrix(matrix1->row_count, matrix2->column_count);
for (int i = 0; i < result->row_count; i++)
{
for (int j = 0; j < result->column_count; j++)
{
result->values[i][j] = 0;
for (int k = 0; k < matrix1->column_count; k++)
{
result->values[i][j] += matrix1->values[i][k] * matrix2->values[k][j];
}
}
}
break;
}
return result;
}
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;
}
我们将在学校进行矩阵运算,释放矩阵时我在某处发生内存泄漏。我已经搜索了几个小时的泄漏,但仍然找不到它。问题是...
您在make_matrix
函数中分配给分配的存储块的大小有问题/错误。请参见以下代码中的注释和建议的更正:
问题在于在adjust_matrixes期间以及在main末尾的释放,在这些情况下,释放非常棘手。在@ adrian-reinstate-monica的大力帮助下(非常感谢。:)),我设法修复了代码的这些部分。