c++ - 我需要释放指向函数的指针吗?

标签 c++ c

对于我正在编写的一些代码,我有一个函数指针。我使用了一段时间,但我不知道是否需要显式释放指针。这是我的代码的简化版本

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/

相关文章:

c# - 在 C++ 中触发事件并在 C# 中处理它们

c++ - 从构造函数中抛出异常

c++ - UWP - ListView 绑定(bind)不显示在 C++/CX 中的 XAML 上

c - 内存分配 : Why this C program works?

c - 将列表写入文件

c++ - long long 和 long int 有什么区别

c++ - 大初始化列表的缺点?

c++ - C++用于使用逗号运算符的多个控制语句

C,从文本文件读取并在 If Else 语句中的 bool 表达式中使用扫描字符

c - 不断收到堆栈粉碎错误