没有名称的函数

Function without name

本文关键字:函数 有名称      更新时间:2023-10-16

我想知道如何调用这个函数?如果它没有名称,我在哪里可以找到它的实现?

extern void (*_malloc_message)(const char* p1, const char* p2, const char* p3, const char* p4);

它不是一个函数。这是一个声明,表示_malloc_message是指向函数的指针,返回类型为void和给定的参数。

为了使用它,您必须为它分配具有该 arity、返回类型和参数类型的函数的地址

然后你会像使用函数一样使用_malloc_message

_malloc_message是一个函数指针。

在代码的某处,您将找到一个函数的定义,其原型如下所示:

void foo (const char* p1, const char* p2, const char* p3, const char* p4);

然后,将函数分配给函数指针,如下所示:

_malloc_message = foo;

并像这样称呼它:

(*_malloc_message)(p1, p2, p3, p4);

问题是为什么你不能直接打电话给foo。 一个原因是您知道 foo 只需要在运行时调用。

_malloc_message在 jemalloc 的 malloc.c 中定义:

这是您可以使用它的方式:

extern void malloc_error_logger(const char *p1, const char *p2, const char *p3, const char *p4)
{
syslog(LOG_ERR, "malloc error: %s %s %s %s", p1, p2, p3, p4);
}
//extern
_malloc_message = malloc_error_logger;

malloc_error_logger()将在各种 malloc 库错误时调用。 malloc.c 有更多细节。