5

Is there any way to get a pointer to the current function, maybe through gcc extensions or some other trickery?

Edit I'm curious whether it is possible to get the function pointer without ever explicitly using the function's name. I thought I had a good reason for wanting this, realized that I didn't really, but am still curious if it is possible.

pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110

4 Answers4

6

This isn't especially portable, but should work on at least some platforms (i.e., Linux and OSX, where I can check the documentation; it definitely doesn't work on Windows which lacks the API):

#include <dlfcn.h>

// ...
void *handle = dlopen(NULL, RTLD_LAZY);
void *thisfunction = handle ? dlsym(handle, __FUNCTION__) : NULL;
if (handle) dlclose(handle); // remember to close!

There are a number of other less-portable shortcuts that work on some platforms but not others. This is also not fast; cache it (e.g., in a local static variable) if you need speed.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • I don't think portability matters for this question since gcc specific extensions were mentioned. So, non-portable hacks are welcome here. :) Although, I would probably never ever use it other than to "wow" some especially nerdy undergrads. – BobbyShaftoe Jun 15 '10 at 21:44
  • It's not *very* hacky… Just a more obscure corner of an API that many programmers don't know about in the first place. – Donal Fellows Jun 15 '10 at 21:53
3

I realise this is likely not what you're after... but it still answers your question as it is currently phrased:

void someFunction()
{
    void (*self)() = someFunction;
}

(Of course, here you could just as well use the identifier someFunction directly in most cases, instead of the function pointer self.)

If, however, you are looking for a means to do the same when you don't know what the current function is called (how could you ever get in such a situation, I wonder?), then I don't know a standard-compliant, portable way of doing this.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • 2
    Certainly, I can use the function name. I was curious whether there was a way to get the function pointer without explicitly using the function name. – pythonic metaphor Jun 15 '10 at 20:29
3

No. In a three-letter answer. In C++ member functions you can have a "this" pointer that does something similar, but there's nothing equivalent in C.

However, since you can't define anonymous functions, there's little need for such a feature.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

Looks like this has been asked before on SO, here is an interesting answer that I have not tested:

Get a pointer to the current function in C (gcc)?

Anyway, there are some interesting extensions, with gcc extensions, are you familiar with the __FUNCTION__ macro?

See what you think about this (this will just get you a string with the name of the function:

#include <stdio.h>
#include <stdlib.h>


void printme(char *foo)
{
    printf("%s says %s\n", __FUNCTION__, foo);
}


int main(int argc, char *argv[])
{

    printme("hey");

    return 0;
}
Community
  • 1
  • 1
BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
  • __FUNCTION__ expands to a string. Does that get me closer? – pythonic metaphor Jun 15 '10 at 20:37
  • @pythonic: No, that doesn't get you closer to the pointer. There's no standard way to go from (string containing function name) to (function pointer), aside from constructing some sort of table first. – David Thornley Jun 15 '10 at 20:41
  • @David: Not portable, but they exist. See my answer (which started as a comment on your comment, but got too long... :-)) – Donal Fellows Jun 15 '10 at 21:37