Accessing C function type

Hi all
I have a function

void myfun(void (*p)() )

with a function pointer passed as argument.
I wonder if there are C function to get the return type and the name of the function the pointer “p” point to.
Thanks in advance


a while ago I’ve found The Function Pointer Tutorials - Index which explains function pointers in C and C++

Hope this helps

No, C is a compiled language. At runtime any pointer looks like any other pointer, they are just machine addresses.

I found it too and was very helpful to understand better function pointer.

OK but in theory if I know the data structure at which the pointer “p” point to I might extract information?

But how will you know if the memory pointed to contains code or data or gabage? There are no tags in data structures. If you want to distinguish data structures with an additional field, as interpreters might want to, you have to arrange it yourself in C.

Remember that C is close to the bare metal. It was intended as a system implementation language to replace assembly language. For many years it was believed that the additional cost of a HLL instead of assembly could not be justified because of the overhead. C and Unix together demonstrated that the overhead could be acceptable (I think the creators of Unix estimated 30%) and was more than repaid in clarity, productivity and ability to write better algorithms. Now nobody seriously argues that we should go back to assembler.

Let me offer a more positive suggestion. You haven’t said what it is you are trying to do. If you are attempting to debug a program by seeing what a function pointer (you hope it is one) is pointing to, then have a look at how debuggers work. They use additional information generated by the build process, such as the symbol map which is a list of symbols and their addresses. The compiler also can be told to generate additional information to help locate source lines. With the help of these, debuggers can correlate addresses to lines in the original code. But the program has to be compiled for debugging, you can’t take a random program and get this info.

thank you ken-yap
it is not the first time that I can appreciate your suggestion.

I’m trying to write code that implements a stack machine, starting from the one described in “the unix programming environment” (I’m not able to find anything better).

It has an evalution stack and a data stack.
I would insert, for debugging purpose, in the function VMexecute(), that unroll the evalution stack, the possibility to print out the feature of the function in execution using the data present in the evalution stack that is the pointer to the function of the virtual machine (i.e. VMadd(), VMpop(),…).

I could insert into the function a passing parameter that tells the function to output its feature (its name, etc…) but is there another way to do this thing?

thank you in advance for your help

You probably want to make use of the compiler generated symbol tables so that the program can do introspection on itself. As I explained, those symbol tables contain name to address mappings. You need to read up on the loader ld and also many of the other binutils programs.

In this case my program should prepare the symbol map, an hash table, but I have to do a reverse look up from the pointer to the symbol.

Using the hashing functions present in Glib I cant find any quick procedure but scroll down the hash table.

Read the mappings into a table, sort, and then do a binary search with the address. Remember though that the addresses in the symbol table are relative to the start of the appropriate segment at execution time. In your case they would be in the code segment.