C 我该如何释放这些马洛克人?

C 我该如何释放这些马洛克人?,c,matrix,malloc,free,C,Matrix,Malloc,Free,我有一个函数,用于将内存分配给矩阵: double **mmalloc(int r, int c){ double **matrix = (double **)malloc((r)*sizeof(double*)); for (int y = 0; y < r; y++){ matrix[y] = (double *)malloc(c*sizeof(double)); } for (int y = 0; y < r; y++){

我有一个函数,用于将内存分配给矩阵:

double **mmalloc(int r, int c){
    double **matrix = (double **)malloc((r)*sizeof(double*));
    for (int y = 0; y < r; y++){
        matrix[y] = (double *)malloc(c*sizeof(double));
    }

    for (int y = 0; y < r; y++){
        for(int x = 0; x < c; x++){
            matrix[y][x] = 0;
        }
    }
    return matrix;
}
double**mmalloc(int r,int c){
双**矩阵=(双**)malloc((r)*sizeof(双*);
对于(int y=0;y
如何释放返回矩阵的所有内存?我有这个函数来释放矩阵。。。我可以释放矩阵的行,但不能释放列

以下是释放函数:

// Free all memory allocated for A 
void mfree(int r, int c, double **A){
    for (int y = 0; y < r; y++){
        free(A[y]);
    }
}
//释放分配给
无效mfree(整数r、整数c、双**A){
对于(int y=0;y
void xfree(整数r、整数c、双**A){
对于(int y=0;y
您需要逐个释放所有行,然后释放最初分配的列(包含所有行)

其中每个
i由(双)列组成

为了完全释放动态分配的数组,请记住这些规则

  • 空闲数必须等于用于分配数组及其数组的malloc数

  • 考虑一下,如果某个东西被释放,它就不再可用,即使它可能是偶然工作的(这样做之后的行为称为未定义行为)。例如,如果您首先
    free(A)
    ,则不应
    free(A[i])
    ,因为
    A
    (包含指针列表的内存空间)不应再被分配/使用

  • 因此,首先释放最里面的元素(“包含”,例如
    A[i]
    ),然后释放“容器”(例如
    A


我会“作弊”,只为您的数据分配一个连续块,然后分配第二个块,让您对行进行数组访问:

int main(){

    int r=3;
    int c=4;

    double* data = malloc(sizeof(double) * r * c);
    double** matrix = malloc(sizeof(double*) * r);

    int i;
    for (i=0;i<r;++i) { /* build the nice syntax accessor */
        matrix[i] = &data[i*c];
    }
    for (i=0;i<(r*c);++i) { /* you can fill/clear the whole matrix in one loop too */
        data[i] = i;
    }    

    // access data through matrix as a normal 2d array
    matrix[2][2] = 1.1;
    int x,y;
    for (x=0;x<r;++x) {
        for (y=0;y<c;++y) {
            printf("[%1.1f]", matrix[x][y]);
        }
        printf("\n");
    }

    // when done
    free(matrix);
    free(data);

    return 0;
}
我甚至不认为调用这两个free的顺序有什么关系(前提是您同时调用这两个free,并且在两者之间没有访问权限),因为一个数组正好指向另一个数组——您对每个数组都有单独的引用

您甚至可以将数据和矩阵一起存储在一个结构或其他东西中,然后使用一个函数获取该结构并释放两个区域(或两个区域,然后释放结构本身)


注意:这似乎是编译和工作,但我不是以写C为生的,在用于生产代码之前先征求第二个意见,以防我遗漏了一些东西

我同意,我使用了ringø的建议我为这个错误感到抱歉。我编写代码时记住A是3D指针,但A是2D指针。我已经更新了我的答案。和往常一样,;)
void xfree(int r, int c, double **A){
    for (int y = 0; y < r; y++){
        free(A[y]);
    }
    free (A);
}
double ** (Initial allocation)
  ↓
(double *)row0 → col0 col1 ...
(double *)row1 → col0 col1 ...
...
int main(){

    int r=3;
    int c=4;

    double* data = malloc(sizeof(double) * r * c);
    double** matrix = malloc(sizeof(double*) * r);

    int i;
    for (i=0;i<r;++i) { /* build the nice syntax accessor */
        matrix[i] = &data[i*c];
    }
    for (i=0;i<(r*c);++i) { /* you can fill/clear the whole matrix in one loop too */
        data[i] = i;
    }    

    // access data through matrix as a normal 2d array
    matrix[2][2] = 1.1;
    int x,y;
    for (x=0;x<r;++x) {
        for (y=0;y<c;++y) {
            printf("[%1.1f]", matrix[x][y]);
        }
        printf("\n");
    }

    // when done
    free(matrix);
    free(data);

    return 0;
}
[0.0][1.0][2.0][3.0]
[4.0][5.0][6.0][7.0]
[8.0][9.0][1.1][11.0]