没有名称的函数
Function without name
我想知道如何调用这个函数?如果它没有名称,我在哪里可以找到它的实现?
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 有更多细节。
相关文章:
- 这个返回元素位置的基于循环的函数有什么问题?
- enable_if继承的成员函数的名称查找错误
- 这个递归函数有什么作用?运行时的复杂性是多少?
- C++类中的友元函数有问题?
- 通过两个嵌套函数传递C++函数的名称
- 在类的第一个/最后一个实例存在之前/之后调用一对函数.有没有更好的方法?
- int** a = new int*[n]();这个函数有什么作用?
- 拥有"受保护的非虚拟析构函数"与"受保护虚拟析构构函数"有什么好处
- 在c++中在类外声明函数有什么好处
- 朋友定义函数的名称空间是什么
- 在MySql中使用字符串操作函数有什么问题
- 有没有办法在 c++ 中按函数的名称动态调用函数
- 我的重载函数有什么问题?
- 在C++项目中管理自由函数有哪些好的做法?
- 函数重载和模板函数有什么区别?哪个更合适?
- "virtual"对C++析构函数有何影响?
- std::函数有性能问题,如何避免?
- 在C/C 中,是否有可能在编译时将函数的名称放入代码中
- 是否有任何方法来检测重载成员函数的名称单独?(不检测名称)
- 是否有可能在GCC中获得正在编译的函数的名称?