简体   繁体   English

释放为指针指针分配的所有空间,而不知道其类型

[英]Free all the space allocated for a pointer-to-pointer, without knowing its type

I have a C program which uses a few pointers such as int ** , float ** , double ** , long ** , etc. At the end, I want to free the space that was allocated for them. 我有一个C程序,它使用一些指针,如int **float **double **long **等。最后,我想释放为它们分配的空间。

To modularize things, I have a function that can take an object of the form pointer-to-pointer and free the memory space allocated for it. 为了模块化,我有一个函数可以获取指针到指针的形式的对象并释放为它分配的内存空间。 For instance, the following, for a variable of type int ** . 例如,对于int **类型的变量,如下所示。

void FreeIntArray(int **A, int length){
    int i;
    for (i = 0; i < length; i ++)
       free(A[i]);
    free(A);
}

However, to free a variable of the form float ** or double ** , I need to write another function that is almost a carbon copy of the one above, except changing int ** to float ** in the function definition. 但是,要释放float **double **形式的变量,我需要编写另一个几乎是上面一个的副本的函数,除了在函数定义中将int **更改为float **

Is a way to design a single function that can free space allocated to any of the following datatypes: int ** , float ** , double ** , long ** ? 是一种设计单个函数的方法,可以释放分配给以下任何数据类型的空间: int **float **double **long **

If you want to punctiliously adhere to the ISO C standard's definition of what is maximally portable, there is no way to do this without repetitive coding. 如果您想要严格遵守ISO C标准对最大可移植性的定义,那么没有重复编码就无法做到这一点。 We can roll the logic into a single function, but that function will need some sort of switch on an integer variable indicating the type, so it can access the type ** pointer correctly as the correct type. 我们可以将逻辑转换为单个函数,但是该函数需要在指示类型的整数变量上进行某种切换,因此它可以正确地访问type **指针作为正确的类型。

We can drop maximal portability and just assume that all pointers have the same representation. 我们可以放弃最大可移植性并假设所有指针具有相同的表示。 (Plus the additional assumption that we won't be bitten by strict aliasing assumptions in the optimizing compiler.) Then we can write it like this: (另外假设我们不会被优化编译器中的严格别名假设所困扰。)然后我们可以像这样写:

void free_array(void *aptr, size_t nelem) {
    void **a = (void **) aptr;
    while (nelem--)
      free(*a++);
    free(aptr);
}

Another alternative is to use a macro: 另一种方法是使用宏:

#define GEN_AFREE(name, type)                   \
   void afree_ ## name (type **a, size_t nelem) \
   {                                            \
     [...]                                      \
   }

GEN_FREE_ARRAY_FUN(int, int)
GEN_FREE_ARRAY_FUN(double, double)
GEN_FREE_ARRAY_FUN(string, char)

Or instead of a defining macro, we could just make a macro that encapsulates looping over an array and calling free: 或者不是定义宏,我们可以只创建一个封装循环数组并调用free的宏:

#define FREE_ARRAY(array, size) do {         \
  size_t iNdEx;                              \
  for (iNdEx = 0; iNdEx < (size); iNdEx++)   \
    free((array)[iNdEx]);                    \
  free(array);                               \
} while (0)

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

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