简体   繁体   English

在C中释放二维数组

[英]Freeing a 2-D array in C

Please bear with me as this is probably a very simple question but I am very new to C. I am trying to malloc a specific array and then free it. 请忍受,因为这可能是一个非常简单的问题,但我对C还是陌生的。我正在尝试malloc一个特定的数组,然后释放它。 However, the line: 但是,这一行:

    M = malloc(N*sizeof(double *));

...doesn't work. ...不起作用。 Can somebody please explain to me why this is not working and what the solution would be? 有人可以向我解释为什么这不起作用以及解决方案是什么吗? Many thanks in advance. 提前谢谢了。

You should free all positions, not just the first. 您应该释放所有职位,而不仅仅是第一职位。 See this example I have wrote. 看到我写的这个例子。

As mentioned from @Grijesh, the allocation is also wrong. 如@Grijesh所述,分配也是错误的。 The example covers allocation too. 该示例也涵盖了分配。 Moreover, I suggest you not to cast the return of malloc ( more ). 此外,我建议您不要转换malloc的返回值( 更多 )。

You have to think the 2D array, as a 1D array, where every cell of it is a pointer to a 1D array. 您必须将2D数组视为1D数组,其中的每个单元格都是指向1D数组的指针。 A picture might help: http://gsamaras.files.wordpress.com/2014/04/array2d-n.png Here, 1D array that holds the pointers is to the left and every cell of it, points to another 1D array. 图片可能会有所帮助: http : //gsamaras.files.wordpress.com/2014/04/array2d-n.png此处,保存指针的1D数组位于左侧,每个单元格都指向另一个1D数组。 How many 1D arrays to the left? 左边有几个一维阵列? As many cells as you have in the left array. 与左数组中的单元格一样多。

Btw, Nelly I think this is not a silly question, it's something that gets beginners into trouble. 顺便说一句,内莉,我认为这不是一个愚蠢的问题,它会使初学者陷入困境。 ;) ;)

EDIT: About your new code, you had to have the same definition and declaration for matrix_free, as well as, call it as you should. 编辑:关于您的新代码,您必须具有与matrix_free相同的定义和声明,并根据需要进行调用。 What's definition, etc. ?? 什么是定义等? Answer . 回答

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define Nmax 9000

double **makeMatrix(int N);
void free_matrix(double **M,int N);

int main(void) {
    int N;

    for (N = 2; N < Nmax; N *= 2) {
        double **L = makeMatrix(N);
        printf("yes \n");
        free_matrix(L, N);
        printf("woo \n");
    }
    return 0;
}

double **makeMatrix(int N) {
    int i, j;
    double **M;

    M = malloc(N * sizeof(double *));
    for (i = 0; i < N; i++)
        M[i] = malloc(N * sizeof(double));

    for (i = 1; i < N; i++) {
        for (j = 1; j < N; j++) {
            M[i][j] = (i) * (j) * M_PI / N;
        }
    }

    return (M);
}

void free_matrix(double **M, int N) {
    int i;
    for (i = 1; i <= N; i++) {
        free(M[i]);
    }
    free(M);
}

And then I receive the youwho output. 然后我收到您的输出。 :) But, it will stop at a certain point, because NMAX is too big! :)但是,它将停止在某个点,因为NMAX太大! Not only NMAX is too big, but N grows really fast ( N *= ). 不仅NMAX太大,而且N增长非常快( N *= )。 Have you done the math in a piece of paper? 你在纸上做过数学运算吗? Too big numbers. 数字太大。 For example, if I do N += , then, I can go until NMAX = 9000 . 例如,如果我执行N += ,那么我可以直到NMAX = 9000 Debug tip : How do I know in which loop it reaches? 调试提示 :我怎么知道它到达哪个循环? I printed out the counter of the loop, like this 我像这样打印出循环的计数器

printf("woo %d\n",N);

Of course, if you feel sure for yourself, then I suggest you learning the debugger. 当然,如果您对自己有把握,那么建议您学习调试器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM