C:我的內存釋放功能可能出現故障

[英]C: Possible glitch in my memory deallocation function


I'm developing a small C library with functions for frequently used operations, mainly to rub up my C skills. In a session of debugging my deallocation function, i saw that the value of a pointer which previously pointed at a two-dimensional matrix isn't NULL.

我正在開發一個小型C庫,其中包含常用操作的功能,主要是為了提高我的C技能。在調試我的釋放函數的會話中,我看到先前指向二維矩陣的指針的值不是NULL。

I know that there is not a way to check the state of a pointer after deallocation (from reading this question), but i assigned a NULL value to this pointer to avoid it to eventually become a dangling pointer.

我知道在重新分配后沒有辦法檢查指針的狀態(從閱讀這個問題),但是我為這個指針分配了一個NULL值,以避免它最終成為懸空指針。

Function code is:

功能代碼是:

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

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

Test code (into another file):

測試代碼(到另一個文件):

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;
}

I expected matfreetest to return status 1, but it returns 0. What is done wrong here?

我期望matfreetest返回狀態1,但它返回0.這里做錯了什么?

EDIT: Code of matmalloc, as requested:

編輯: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;
}
}

1 个解决方案

#1


3  

The "M" that is parameter to matfree is not the same as the one you have in the calling program. It would be better to name it differently to avoid ambiguity.

作為matfree參數的“M”與您在調用程序中的參數不同。最好不同地命名以避免歧義。

You should have:

你應該有:

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;
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/07/19/725c97ab156620e0e3c6a372be1f5afc.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com