Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Accessing C function type

  1. #1

    Default Accessing C function type

    Hi all
    I have a function
    Code:
    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
    Saluti
    R

  2. #2
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    301

    Default Re: Accessing C function type

    Hi,

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

    Hope this helps

  3. #3
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: Accessing C function type

    Quote Originally Posted by roberto60 View Post
    Hi all
    I have a function
    Code:
    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
    No, C is a compiled language. At runtime any pointer looks like any other pointer, they are just machine addresses.

  4. #4

    Default Re: Accessing C function type

    Quote Originally Posted by Monex View Post
    Hi,

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

    Hope this helps
    I found it too and was very helpful to understand better function pointer.
    Saluti
    R

  5. #5

    Default Re: Accessing C function type

    Quote Originally Posted by ken_yap View Post
    No, C is a compiled language. At runtime any pointer looks like any other pointer, they are just machine addresses.
    OK but in theory if I know the data structure at which the pointer "p" point to I might extract information?
    Saluti
    R

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: Accessing C function type

    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.

  7. #7
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: Accessing C function type

    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.

  8. #8

    Default Re: Accessing C function type

    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
    Saluti
    R

  9. #9
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: Accessing C function type

    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.

  10. #10

    Default Re: Accessing C function type

    Quote Originally Posted by ken_yap View Post
    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.
    Saluti
    R

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •