12

I would like to know how to call this function? And where can i find it's implementation if it doesn't have name?

extern void (*_malloc_message)(const char* p1, const char* p2, const char* p3, const char* p4);
Rene Knop
  • 1,788
  • 3
  • 15
  • 27
Pozzi Userpic
  • 347
  • 8
  • 30

3 Answers3

22

It isn't a function. It's a declaration saying that _malloc_message is a pointer to a function, with return type void and the parameters as given.

In order to use it, you'd have to assign to it the address of a function with that arity, return type, and parameter types.

Then you'd use _malloc_message as if it were a function.

Eevee
  • 47,412
  • 11
  • 95
  • 127
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

_malloc_message is a function pointer.

Somewhere in the code you will find the definition of a function whose prototype is like this:

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

Then you assign the function to the function pointer like this:.

_malloc_message = foo;

and call it like this:

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

The question is why you cannot call foo directly. One reason is that you know that foo needs to be called only at runtime.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • It is not my code. I'm trying to learn advanced C++ by reading somebody else code. There are all those steps except a calling one. That is interesting, maybe the function is no longer used in that code and author forgot to delete it. – Pozzi Userpic Sep 03 '18 at 14:33
  • 1
    @Chyu • there are several good books on learning advanced C++. Here is a curated list of books: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Eljay Sep 03 '18 at 14:52
  • 2
    @Chyu The call might be hidden in a macro. Also, since it's declared `extern`, it might be called from a different compilation unit. – Barmar Sep 03 '18 at 22:14
1

_malloc_message is defined in malloc.c of jemalloc:

This is how you may use it:

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() would be called on various malloc library errors. malloc.c has more details.

Deepak Mohanty
  • 121
  • 1
  • 2