【问题标题】:Can't deallocate 2D array with free in C无法在 C 中释放 2D 数组
【发布时间】:2015-06-17 08:50:27
【问题描述】:

我试图解除分配我在MatrizCrea(n,m) 中使用MatrizLibera(v) 创建的矩阵,但free() 都告诉我存在类型冲突。

我已经按照多个来源完成了这段代码,所以我很不确定为什么会发生这个错误。

header.h

typedef struct Matriz {int n, m, **d;} Matriz;

Matriz MatrizCrea(int n, int m);
void MatrizLibera(Matriz v);



body.c

Matriz MatrizCrea(int n, int m) {
    Matriz mat;
    mat.n = n;
    mat.m = m;

    int ** val = (int*)malloc(n*sizeof(int*));
    int i = 0;
    for (; i<n;i++) {
        val[i] = (int*)malloc(m*sizeof(int*));
    }

    mat.d = val;
    return mat;
}

void MatrizLibera(Matriz v) {
    int i = 0;
    for (; i<v.n; i++) {
        int *a = v.d[i];
        free(a);
    }
    free(v);
}

我应该如何释放二维数组?

提前致谢。

【问题讨论】:

  • val[i] = (int*)malloc(m*sizeof(int*)); 看起来不对.. 建议 val[i] = malloc(m*sizeof *(val[i]));(错误类型)
  • 你确定free(v)?不应该是free(v.d)吗?此外,作为一种优化,这些函数应该返回一个指针并接受一个指针参数。
  • int ** val = (int**)malloc(n*sizeof(int*));, val[i] = (int*)malloc(m*sizeof(int));, free(v.d);
  • @chux 感谢您的回答。不幸的是,我仍然收到相同的“免费冲突类型()”错误。
  • @user3121023 确实如此,但我在调用它时将它存储在另一个矩阵中,.n 和 .m,直接传递给新矩阵,唯一需要作为指针的是.d,我相信。 (我做matriz name = MatrizCrea(4,5);

标签: c arrays types free dealloc


【解决方案1】:

试试下面的

Matriz MatrizCrea( int n, int m ) 
{
    Matriz mat;

    mat.n = n;
    mat.m = m;

    mat.d = malloc( n * sizeof( int* ) );

    int i = 0;
    for ( ; i < n; i++ ) 
    {
        mat.d[i] = malloc( m * sizeof( int ) );
    }

    return mat;
}

void MatrizLibera( Matriz *mat ) 
{
    if ( mat->d != NULL )
    {
        int i = 0;
        for ( ; i < mat->n; i++ ) 
        {
            free( mat->d[i] );
        }

        free( mat->d );

        mat->d = NULL;
        mat->n = 0;
        mat->m = 0;
    }
}

您也可以在函数MatrizCrea中插入一个代码来检查内存是否分配成功。

【讨论】:

  • 嗯,还是一样的问题,冲突的类型不允许我编译。我希望不要在MatrizLibera 的声明中使用指针。谢谢
  • @Xaring 没有任何冲突的类型。
  • 我的机器里有 :S 谢谢你的帮助,我重新安装编译器后试试。
  • @Xaring 你把#include &lt;stdlib.h&gt; 放了吗?
【解决方案2】:

在您的 MatrizCrea 中,您在第二个 malloc 中出错: 这个

for (; i<n;i++) {
        val[i] = (int*)malloc(m*sizeof(int*));
    }

你的 sizeof 应该是 int 而不是 int 指针

编辑:另一个错误:

   int ** val = (int*)malloc(n*sizeof(int*));

类型不匹配应该是:

    int ** val = (int**) malloc(n*sizeof(int*));

对于我将使用的解除分配:

for(i = 0; i < v.n ; i++) {
    free(v[i]);
}
free(v);

【讨论】:

  • 它应该是v.d[i]v.d,因为我首先将数组存储在v.d 中......仍然出现冲突类型错误。我想我会重新安装我的编译器:S
  • @Xaring 我不认为这是你的编译器。您可能会检查的内容:您在正确的位置有 #include &lt;stdlib.h&gt; 吗?您将代码编译为 C 还是 C++?使用 gcc,请确保使用 gcc 编译,而不是使用 g++。并非所有有效的 C 代码都是有效的 C++,并且您在 malloc() 调用中的强制转换暗示您可能正在将代码编译为 C++ 代码。
  • @cmaster 这是我的编译器,一定是。我使用了一个在线编译器,它很好,是的,我使用的是 GCC 而不是 G++。
【解决方案3】:

header.h

// dont typedef struct definitions
struct Matriz {int n, m, **d;};

// pass and return pointers
struct Matriz* MatrizCrea(int n, int m);
void MatrizLibera(struct Matriz* v);

body.c

struct Matriz* MatrizCrea(int n, int m) 
{
    // indent code blocks
    struct Matriz* mat = malloc( sizeof struct Matriz );
    if (NULL == mat )
    {
        perror( "malloc for matrix struct failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    mat->n = n;
    mat->m = m;

    if( NULL == (mat->d = malloc(n*sizeof(int*)) )
    {
        perror( "malloc for d failed" );
        free(mat);
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    memset( mat->d, '\0', n*sizeof(int)); 

    int i = 0;
    for (; i<n;i++) 
    {
        if( NULL == (mat->d[i] = malloc(m*sizeof(int)) )
        {
            perror( "malloc for d[x] failed" );
            for(i = 0; i < m; i++ ) { free(mat->d[i]); }
            free( mat->d );
            free( mat );
            exit( EXIT_FAILURE );
        }

        // implied else, malloc successful
    } // end for
    return mat;
} // end function: MatrizCrea



void MatrizLibera(struct Matriz* v) 
{
    int i = 0;
    for (; i<v->n; i++) 
    {
        free( v->d[i]);
    }
    free( v->d );

    free(v);
} // end function: MatrizLibera

【讨论】:

  • 代码需要包含用于 malloc() 和 free() 的 string.h。代码需要包含用于 exit() 和 EXIT_FAILURE 的 stdlib.h
猜你喜欢
  • 1970-01-01
  • 2022-01-05
  • 2015-03-04
  • 1970-01-01
  • 1970-01-01
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
相关资源
最近更新 更多