Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
矩阵的c函数free()_C_Memory Management - Fatal编程技术网

矩阵的c函数free()

矩阵的c函数free(),c,memory-management,C,Memory Management,我正在为学校做一个纯c的项目 char** init_matrix ( ) { printf ( "%s\n", __PRETTY_FUNCTION__ ); char** temp_matrix; temp_matrix = ( char ** ) malloc ( CHAR_BIT * sizeof (char* ) ); for ( int i = 0; i < CHAR_BIT; i ++ ) { *( temp_matrix

我正在为学校做一个纯c的项目

char** init_matrix ( )
{
    printf ( "%s\n", __PRETTY_FUNCTION__ );
    char** temp_matrix;
    temp_matrix = ( char ** ) malloc ( CHAR_BIT * sizeof (char* ) );
    for ( int i = 0; i < CHAR_BIT; i ++ )
    {
        *( temp_matrix + i ) = ( char * ) calloc ( CHAR_BIT, sizeof (char ) );
    }

    return temp_matrix;
}
自由级联到所有指向的指针吗

free(mat);
还是应该为定义的每个指针再次执行for循环

自由级联到所有指向的指针吗

free(mat);
没有

我应该为定义的每个指针再次执行for循环吗

一般规则是:

每1次调用
malloc()
或其朋友(
calloc()
realloc()
strdup()
),1次调用
free()


在已经重新分配的内存块上调用
realloc()
不算。

你所说的“纯”C是什么意思(可能与“不纯”C相比)?我看到了
malloc()
calloc()
的使用,它们不符合C语言标准。你不应该投出结果
free()
不级联。如果要编写自己的函数来释放矩阵,应使用
for
循环来释放定义的每个指针。“free”函数不会“级联”,代码必须将任何内存分配函数调用返回的每个指针传递到
free()