gpt4 book ai didi

C:我的内存释放功能可能出现故障

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:06 25 4
gpt4 key购买 nike

我正在开发一个包含常用操作函数的小型 C 库,主要是为了提高我的 C 技能。在调试我的释放函数的过程中,我看到先前指向二维矩阵的指针的值不是 NULL

我知道在解除分配后没有办法检查指针的状态(来自 reading 这个问题),但我为这个指针分配了一个 NULL 值以避免它最终成为悬空指针。

函数代码为:

void matfree(void **M, int m){
int i;

for(i = 0; i < m; i ++){
free(M[i]);
M[i] = NULL;
}
free(M);
M = NULL;
}

测试代码(到另一个文件):

int matfreetest(){
int **M = NULL;

M = (int **) matmalloc(0, 2, 2);
if(*M == NULL){
printf("Here 1! \n");
return 0;
}
matfree(M, 2);
if(*M == NULL){
return 1;
}
else{
printf("Here 2! \n");
return 0;
}
}

int main(){
int status;

status = matmalloctest();
printf("matmalloctest status = %d \n", status);
status = matfreetest();
printf("matfreetest status = %d \n", status);
system("PAUSE");
return 1;
}

我希望 matfreetest 返回状态 1,但它返回 0。这里做错了什么?

编辑:matmalloc代码,按要求:

/* Memory allocator for any type and size of matrix. 
Arguments:
- int type: identifier of the matrix' type
- int m: number of matrix rows
- int n: number of matrix columns
*/
void **matmalloc(int type, int m, int n){
int i;
void **M;

/* Check data type the do the proper allocation. */
if(type == INTEGER){ //int
M = (int **) malloc(m*sizeof(int *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (int *) malloc(n*sizeof(int));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == SINTEGER){ //short int
M = (short int **) malloc(m*sizeof(short int *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (short int *) malloc(n*sizeof(short int));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == LINTEGER){ //long int
M = (long int **) malloc(m*sizeof(long int *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (long int *) malloc(n*sizeof(long int));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == FL){ //float
M = (float **) malloc(m*sizeof(float *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (float *) malloc(n*sizeof(float));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == DOUB){ //double
M = (double **) malloc(m*sizeof(double *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (double *) malloc(n*sizeof(double));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else if(type == LDOUB){ //long double
M = (long double **) malloc(m*sizeof(long double *));
if(M == NULL){
printf("Allocation failed! \n");
return NULL;
}
for(i = 0; i < m; i ++){
M[i] = (long double *) malloc(n*sizeof(long double));
if(M[i] == NULL){
printf("Allocation failed! \n");
return NULL;
}
}
printf("Allocation completed! \n");
return M;
}
else{
printf("Incorrect type of matrix data. Only numeric types are accepted. \n");
return NULL;
}
}

最佳答案

作为 matfree 参数的“M”与调用程序中的参数不同。最好用不同的名称来避免歧义。

你应该:

M = matfree(M, 2); // Frees M and reassigns it to NULL

void **matfree(void **X, int m)
{
int i;
for(i = 0; i < m; i ++)
{
free(X[i]);
X[i] = NULL;
}
free(X);
return NULL;
}

关于C:我的内存释放功能可能出现故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11568362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com