Determining to which function a pointer is pointing in C?

后端 未结 9 991
情话喂你
情话喂你 2021-02-01 12:25

I have a pointer to function, assume any signature. And I have 5 different functions with same signature.

At run time one of them gets assigned to the pointer, and that

相关标签:
9条回答
  • 2021-02-01 13:08

    Generally, in C such things are not available to the programmer.

    There might be system-specific ways of getting there by using debug symbols etc., but you probably don't want to depend on the presence of these for the program to function normally.

    But, you can of course compare the value of the pointer to another value, e.g.

    if (ptr_to_function == some_function)
        printf("Function pointer now points to some_function!\n");
    
    0 讨论(0)
  • 2021-02-01 13:10

    Not at all - the symbolic name of the function disappears after compilation. Unlike a reflective language, C isn't aware of how its syntax elements were named by the programmer; especially, there's no "function lookup" by name after compilation.

    You can of course have a "database" (e.g. an array) of function pointers that you can compare your current pointer to.

    0 讨论(0)
  • 2021-02-01 13:11
    1. set your linker to output a MAP file.
    2. pause the program
    3. inspect the address contained in the pointer.
    4. look up the address in the MAP file to find out which function is being pointed to.
    0 讨论(0)
  • 2021-02-01 13:14

    To know where a function pointer points is something you'll have to keep track of with your program. Most common is to declare an array of function pointers and use an int variable as index of this array.

    That being said, it is nowadays also possible to tell in runtime which function that is currently executed, by using the __func__ identifier:

    #include <stdio.h>
    
    typedef const char* func_t (void);
    
    const char* foo (void)
    {
      // do foo stuff
      return __func__;
    }
    
    const char* bar (void)
    {
      // do bar stuff
      return __func__;
    }
    
    int main (void)
    {
      func_t* fptr;
    
      fptr = foo;
      printf("%s executed\n", fptr());
    
      fptr = bar;
      printf("%s executed\n", fptr());
    
      return 0;
    }
    

    Output:

    foo executed
    bar executed
    
    0 讨论(0)
  • 2021-02-01 13:16

    You will have to check which of your 5 functions your pointer points to:

    if (func_ptr == my_function1) {
        puts("func_ptr points to my_function1");
    } else if (func_ptr == my_function2) {
        puts("func_ptr points to my_function2");
    } else if (func_ptr == my_function3) {
        puts("func_ptr points to my_function3");
    } ... 
    

    If this is a common pattern you need, then use a table of structs instead of a function pointer:

    typedef void (*my_func)(int);
    
    struct Function {
        my_func func;
        const char *func_name;
    };
    
    #define FUNC_ENTRY(function) {function, #function}
    
    const Function func_table[] = {
        FUNC_ENTRY(function1),
        FUNC_ENTRY(function2),
        FUNC_ENTRY(function3),
        FUNC_ENTRY(function4),
        FUNC_ENTRY(function5)
    }
    
    struct Function *func = &func_table[3]; //instead of func_ptr = function4;
    
    printf("Calling function %s\n", func->func_name);
    func ->func(44); //instead of func_ptr(44);
    
    0 讨论(0)
  • 2021-02-01 13:16

    A pointer to a C function is an address, like any pointer. You can get the value from a debugger. You can cast the pointer to any integer type with enough bits to express it completely, and print it. Any compilation unit that can use the pointer, ie, has the function name in scope, can print the pointer values or compare them to a runtime variable, without touching anything inside the functions themselves.

    0 讨论(0)
提交回复
热议问题