C-找不到内存泄漏
我们要为学校做矩阵运算,当释放矩阵时,我在某处有内存泄漏。 我已经找了几个小时的漏洞,但还是找不到。问题可能是在计算操作期间释放矩阵之间的某个地方 标准文本: 1.2(行数和列数) 6.4(矩阵值) + 1 2 -6 7 + 1 2 -6-4 瓦尔格林:C-找不到内存泄漏,c,matrix,memory,memory-leaks,allocation,C,Matrix,Memory,Memory Leaks,Allocation,我们要为学校做矩阵运算,当释放矩阵时,我在某处有内存泄漏。 我已经找了几个小时的漏洞,但还是找不到。问题可能是在计算操作期间释放矩阵之间的某个地方 标准文本: 1.2(行数和列数) 6.4(矩阵值) + 1 2 -6 7 + 1 2 -6-4 瓦尔格林: ==1480== HEAP SUMMARY: ==1480== in use at exit: 56 bytes in 3 blocks ==1480== total heap usage: 17 allocs, 14 frees,
==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消息,它们是捷克语的。)
- 只包括重要部分
#包括
#包括
#定义POINTERSIZE 8
#定义MaxMatrix 100
类型定义结构
{
int行计数、列计数;
整数**值;
}基质;
无空洞矩阵(矩阵*矩阵)
{
对于(int i=0;irow\u count;i++)
{
自由(矩阵->值[i]);
}
自由(矩阵->值);
自由(矩阵);
}
矩阵*生成矩阵(整数行数、整数列数)
{
矩阵*矩阵=malloc(列数*行数*2*点化);***(此处有问题)***
矩阵->行数=行数;
矩阵->列计数=列计数;
矩阵->值=malloc(列计数*行计数*指针化);
对于(int i=0;i值[i]=malloc(列计数*sizeof(int));
}
收益矩阵;
}
矩阵*calculate_运算(矩阵*matrix1,矩阵*matrix2,字符运算符)
{
如果(操作是否可能(matrix1,matrix2,运算符)!=1)
{
fprintf(标准,“错误:Chybny vstup!\n”);
自由矩阵(matrix1);
自由矩阵(matrix2);
出口(100);
}
矩阵*结果;
开关(操作员)
{
格“+”:
结果=生成矩阵(矩阵1->行计数,矩阵1->列计数);***(这里有问题)***
对于(int i=0;irow\u count;i++)
{
对于(int j=0;jcolumn\u count;j++)
{
结果->值[i][j]=matrix1->值[i][j]+matrix2->值[i][j];
}
}
打破
}
矩阵*加载矩阵()
{
int行计数、列计数、输入检查;
输入检查=scanf(“%d%d”、&行计数和列计数);
如果(input_check!=2 | | row_count您在make_matrix
函数中为分配的内存块提供的大小存在问题/错误。请参阅以下代码中的注释和建议的更正:
Matrix*生成矩阵(int行计数、int列计数)
{
//矩阵*矩阵=malloc(列计数*行计数*2*点化);
Matrix*Matrix=malloc(sizeof(Matrix));//内存足够容纳1个“Matrix”
矩阵->行数=行数;
矩阵->列计数=列计数;
//矩阵->值=malloc(列计数*行计数*指针化);
矩阵->值=malloc(row_count*POINTERSIZE);//每行仅一个指针
对于(int i=0;i值[i]=malloc(列计数*sizeof(int));
}
收益矩阵;
}
另一个问题(泄漏的可能原因)在main
末尾的while
循环的大部分运行中,您没有释放结果
内存。解决这一问题的一种方法是对calculate\u操作
函数的第一个参数使用第二个指针,将其设置为循环之前的初始值,然后更新在每次运行时将其设置为最新的结果
:
/。。。
int指数=0;
矩阵*param1=矩阵[index];//使用“param1”作为第一个操作数指针
矩阵*result=NULL;//这将仅用于结果
while(运算符[索引]!=0)
{
结果=计算_运算(参数1,矩阵[index+1],运算符[index]);
如果(index!=0)在第一个循环中释放_矩阵(param1);//则在下面释放param1。。。
自由_矩阵(矩阵[index]);/…因为它被设置为矩阵[0]
param1=result;//我们将“param1”设置为指向计算结果。
索引++;
}
自由矩阵(矩阵[索引]);
如果(result!=NULL){//以防循环从未运行!
打印矩阵(结果);
自由矩阵(结果);
}
返回0;
问题出在调整矩阵期间和main结尾的释放过程中,释放过程非常棘手。在@adrian Restore monica(非常感谢:)的大力帮助下,我成功地修复了代码的这些部分
void adjust_matrixes(Matrix *matrixes[], char operators[])
{
for (int i = 0; i < MAXMATRIXES; i++)
{
Matrix *param1 = matrixes[i];
while (operators[i] == '*' && operators[1])
{
matrixes[i] = calculate_operation(param1, matrixes[i + 1], '*');
free_matrix(param1);
param1 = matrixes[i];
free_matrix(matrixes[i + 1]);
//Shifting next operator to the position
//of current operator, so that
//while loop won't repeat forever.
operators[i] = operators[i + 1];
//Shifting every matrix & operator behind current ones
//one step closer.
for (int j = i + 1; j < MAXMATRIXES - 1; j++)
{
matrixes[j] = matrixes[j + 1];
operators[j] = operators[j + 1];
}
}
}
}
void adjust_矩阵(矩阵*矩阵[],字符运算符[])
{
对于(int i=0;i
intmain(intargc,char*argv[])
{
字符运算符[MaxMatrix];
对于(int i=0;ivoid adjust_matrixes(Matrix *matrixes[], char operators[])
{
for (int i = 0; i < MAXMATRIXES; i++)
{
Matrix *param1 = matrixes[i];
while (operators[i] == '*' && operators[1])
{
matrixes[i] = calculate_operation(param1, matrixes[i + 1], '*');
free_matrix(param1);
param1 = matrixes[i];
free_matrix(matrixes[i + 1]);
//Shifting next operator to the position
//of current operator, so that
//while loop won't repeat forever.
operators[i] = operators[i + 1];
//Shifting every matrix & operator behind current ones
//one step closer.
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];
//Scanning matrixes and operators from stdin.
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);
//The calculations themselves in
//adjusted array of matrixes
int index = 0;
Matrix *param1 = matrixes[index];
Matrix *result = NULL;
while (operators[index] != 0)
{
result = calculate_operation(param1, matrixes[index + 1], operators[index]);
if (index != 0)
free_matrix(param1);
free_matrix(matrixes[index]);
param1 = result;
index++;
}
free_matrix(matrixes[index]);
print_matrix(result);
free_matrix(result);
return 0;
}