没有名字的函数
Function without name
我想知道如何调用这个函数?如果它没有名称,我在哪里可以找到它的实现?
extern void (*_malloc_message)(const char* p1, const char* p2, const char* p3, const char* p4);
这不是函数。这是一个声明 _malloc_message
是一个指向函数的指针,return 类型 void
和给定的参数。
为了使用它,您必须为其分配具有该参数、return 类型和参数类型的函数的 地址。
然后您可以像使用函数一样使用 _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 有更多详细信息。
我想知道如何调用这个函数?如果它没有名称,我在哪里可以找到它的实现?
extern void (*_malloc_message)(const char* p1, const char* p2, const char* p3, const char* p4);
这不是函数。这是一个声明 _malloc_message
是一个指向函数的指针,return 类型 void
和给定的参数。
为了使用它,您必须为其分配具有该参数、return 类型和参数类型的函数的 地址。
然后您可以像使用函数一样使用 _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 有更多详细信息。