You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言程序getRow、invert函数引发内存泄漏无法释放问题求助

内存泄漏问题定位与修复方案

1. 明确泄漏点

  • 泄漏点1:外层j循环开头分配的currentRow、currentRow2未释放
    你在for(int j =0;j<size;j++)开头调用两次getRow分配了两个堆内存,三个分支都没有正确释放:
    • 走matrix[j][j]==1分支直接continue,两个内存直接泄漏
    • 走matrix[j][j]==0分支,仅释放了otherRow和otherRow2,开头分配的currentRow、currentRow2未释放
    • 走else分支,currentRow、currentRow2被重新赋值后未释放原始内存
  • 泄漏点2:内层循环分配的pivotRow、pivotRow2未释放
    内层i循环中调用getRow获取的pivotRow、pivotRow2使用完成后没有调用free,直接丢失指针
  • 潜在隐患:getRow函数malloc类型错误
    getRow中malloc(sizeof(double*)*size)应该改为malloc(sizeof(double)*size),你要分配的是double数组,不是double指针数组,虽然当前平台下大小一致,但属于类型错误,后续更换运行环境可能触发非法访问。

2. 修复代码示例

修复后的invert函数

double ** invert(double **matrix, int size){
    double ** identity = allocateMatrix(size, size);
    for(int i =0;i<size;i++){
        for(int j =0;j<size;j++){
            identity[i][j] = (i==j) ? 1.0 : 0.0;
        }
    }
    
    for(int j =0;j<size;j++){
        double * currentRow = getRow(size, j,matrix);
        double * currentRow2 = getRow(size, j,identity);
    
        if(matrix[j][j]==0){
            printMatrix(matrix,size,size);
            int a = j+1;
            while(a<size){
                if(matrix[a][j]!=0) break;
                a++;
            }
    
            double * otherRow2 = getRow(size, a, identity);
            otherRow2 = multiplyRowByScalar(size, otherRow2, 1/matrix[a][j]);
            otherRow2 = addRows(size,currentRow2, otherRow2);
            identity = setRow(size, j, identity, otherRow2);
    
            double * otherRow = getRow(size, a, matrix);
            otherRow = multiplyRowByScalar(size, otherRow, 1/matrix[a][j]);
            otherRow = addRows(size,currentRow, otherRow);
            matrix = setRow(size, j, matrix, otherRow);
    
            free(otherRow);
            free(otherRow2);
            // 释放开头分配的currentRow、currentRow2
            free(currentRow);
            free(currentRow2);
        }
        else if(matrix[j][j]==1){
            // 释放开头分配的currentRow、currentRow2再continue
            free(currentRow);
            free(currentRow2);
            continue;
        }
        else{
            double * oldRow = currentRow2;
            currentRow2 = multiplyRowByScalar(size, currentRow2, 1/matrix[j][j]);
            identity = setRow(size, j, identity, currentRow2);
            free(oldRow); // 释放原始currentRow2内存

            double * oldRow2 = currentRow;
            currentRow = multiplyRowByScalar(size, currentRow, 1/matrix[j][j]);
            matrix = setRow(size, j, matrix, currentRow);
            free(oldRow2); // 释放原始currentRow内存
        }
    
        for(int i=0;i<size;i++){
            double * currentRow = getRow(size, i,matrix);
            double * currentRow2 = getRow(size, i,identity);
    
            if(i!=j){
                if(matrix[i][j]!=0){
                    double * pivotRow2 = getRow(size, j,identity);
                    pivotRow2 = multiplyRowByScalar(size, pivotRow2, -matrix[i][j]);
                    double * oldRow2 = currentRow2;
                    currentRow2 = addRows(size,pivotRow2, currentRow2);
                    identity = setRow(size, i, identity, currentRow2);
                    free(oldRow2); // 释放原始currentRow2
                    free(pivotRow2); // 释放pivotRow2

                    double * pivotRow = getRow(size, j,matrix);
                    pivotRow = multiplyRowByScalar(size, pivotRow, -matrix[i][j]);
                    double * oldRow = currentRow;
                    currentRow = addRows(size,pivotRow, currentRow);
                    matrix = setRow(size, i, matrix, currentRow);
                    free(oldRow); // 释放原始currentRow
                    free(pivotRow); // 释放pivotRow
                }
            }
    
            free(currentRow);
            free(currentRow2);
        }
    }
    return identity;
}

修复后的getRow函数

double *getRow(int size, int rowNum, double **matrix){
  // 修复类型错误,改为申请double大小的内存
  double *row = malloc(sizeof(double)*size);
  for(int i =0;i<size;i++){
    row[i] = matrix[rowNum][i];
  }
  return row;
}

3. 验证说明

修复后所有getRow分配的内存都有对应的free调用,且修正了类型隐患,重新编译后用内存检测工具(如valgrind)运行即可确认泄漏已解决。

内容的提问来源于stack exchange,提问作者Gope S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 03:54:03