对于我正在编写的一些代码,我有一个函数指针。我使用了一段时间,但我不知道是否需要显式释放指针。这是我的代码的简化版本
struct Person
{
void (*action) ();
}
void action()
{
printf("Action has been called\n");
}
int main()
{
Person p;
p->action = &action;
// My question is whether or not I need to do this
// free (p->action);
// p->action = NULL;
}
谢谢。
最佳答案
I have a function pointer. I use it for some time, but I don't know whether I need to explicitly deallocate the pointer.
不。您不需要使用free()
释放任何东西,您从来没有使用malloc()
或类似的方法进行动态分配.
事实上,使用 malloc()
创建函数指针几乎是不可能的。
关于c++ - 我需要释放指向函数的指针吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37473522/