简体   繁体   English

免费的函数指针数组

[英]Free array of function pointers

If an array of function pointers is statically allocated, do I still need to free() it? 如果静态分配了函数指针数组,我是否还需要free() Lets say I have this code: 可以说我有以下代码:

typedef void (*test_ptr)(void);

int main(void) 
{
    test_ptr test[3];
    test[0] = a;
    test[1] = b;
    test[2] = c;
}

So, when I would be done with it, would I have to free all the pointers, maybe like so: 因此,当我完成该操作后,是否需要释放所有指针,也许像这样:

for(int i = 0; i < 3; i++) {
    free(test[i]);
}

Or is it automatically de-allocated when function ends, like other arrays? 还是像其他数组一样在函数结束时自动取消分配?

Absolutely not! 绝对不!

An array allocated like that is located on the stack, and free will fail horribly if you try it. 这样分配的数组位于堆栈上,如果您尝试使用free ,则free将会严重失败。

As an aside, the array you show is not "static". 顺便说一句,您显示的数组不是 “静态”的。 I think you mean "local", or even "auto" (although that can be confused with a C++ concept). 我认为您的意思是“本地”,甚至“自动”(尽管可以与C ++概念混淆)。

Aside no. 除了没有 2: allocation on the stack means that you should not allocate large arrays this way. 2:在堆栈上分配意味着您不应以这种方式分配大数组 The stack typically has limited capacity, so anything large should be allocated via malloc . 堆栈通常具有有限的容量,因此任何大的都应通过malloc分配。

So, when I would be done with it, would I have to free all the pointers 因此,当我完成该操作后,是否必须释放所有指针

No. You must not because free(ptr) is used only when pointer ptr is previously returned by any of malloc family functions. 不可以。因为只有当malloc系列函数中的任何一个指针先前返回了指针ptr时,才使用free(ptr)
Passing free a pointer to any other object (like a variable or array element) causes undefined behaviour . free一个指向任何其他对象(如变量或数组元素)的指针会导致未定义的行为

C11: 7.22.3.3 The free function: C11:7.23.2.3的免费功能:

[...] If ptr is a null pointer, no action occurs. [...]如果ptr是空指针,则不会发生任何操作。 Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc , the behavior is undefined . 否则,如果参数与内存管理函数先前返回的指针不匹配,或者如果通过调用freerealloc释放了空间,则该行为是undefined


Or is it automatically de-allocated when function ends, like other arrays? 还是像其他数组一样在函数结束时自动取消分配?

Yes. 是。 It will no longer exist once its enclosing function returns. 一旦其封闭函数返回,它将不再存在。

Automatic local variables like the ones in your case are destroyed as soon as the function in which they are created gets over. 自动局部变量(如您所用的局部变量)将在创建它们的函数结束后立即销毁。 In your case, they will be destroyed when the main() function gets over. 就您而言,它们将在main()函数结束时被销毁。

So you don't have to explicitly free() those variables. 因此,您不必显式地free()这些变量。

Does that code own the pointed-to-object, and was it allocated with one of the allocation-functions corresponding to free ( malloc realloc calloc )? 该代码是否拥有指向对象的代码,并且被分配了与freemalloc realloc calloc )相对应的分配函数之一?

If so, free it at the end. 如果是这样,请在最后free它。

If not, desist. 如果没有,请停止。

You are clearly in the second case... 您显然在第二种情况下...

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

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