Function pointer cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bartc

    Function pointer cast

    I have a variable like this:

    int *p;

    Sometimes this points to an int location containing the address of a
    void(void) function.

    How can I cast it so that I can call that function?

    I've tried casting to void **(void). The compiler keeps saying such a cast
    is illegal.

    (It should compile to something like this in x86 code:
    mov esi,[p]
    call [esi]
    )

    Thanks,

    -- Bartc



  • Dann Corbit

    #2
    Re: Function pointer cast

    "Bartc" <bc@freeuk.comw rote in message
    news:nhONj.1011 3$yD2.6254@text .news.virginmed ia.com...
    >I have a variable like this:
    >
    int *p;
    >
    Sometimes this points to an int location containing the address of a
    void(void) function.
    >
    How can I cast it so that I can call that function?
    >
    I've tried casting to void **(void). The compiler keeps saying such a cast
    is illegal.
    >
    (It should compile to something like this in x86 code:
    mov esi,[p]
    call [esi]
    )
    According to the C99 standard, it may be possible as a common extension:
    "J.5.7 Function pointer casts

    1 A pointer to an object or to void may be cast to a pointer to a function,
    allowing data to

    be invoked as a function (6.5.4).

    2 A pointer to a function may be cast to a pointer to an object or to void,
    allowing a function to be inspected or modified (for example, by a debugger)
    (6.5.4)."

    Show the actual code you are trying to use.




    ** Posted from http://www.teranews.com **

    Comment

    • Eric Sosman

      #3
      Re: Function pointer cast

      Bartc wrote:
      I have a variable like this:
      >
      int *p;
      >
      Sometimes this points to an int location containing the address of a
      void(void) function.
      >
      How can I cast it so that I can call that function?
      >
      I've tried casting to void **(void). The compiler keeps saying such a cast
      is illegal.
      >
      (It should compile to something like this in x86 code:
      mov esi,[p]
      call [esi]
      )
      The call would look like

      ((void(*)(void) )*p)();

      .... which might be more readable with a typedef (I dislike
      typedefs that name object pointer types, but for function
      pointers they are often helpful):

      typedef void (*VoidOfVoid)(v oid);
      ((VoidOfVoid)*p )();

      Be aware that treating an int value as a function pointer
      is not something the Standard says must work. It is quite
      likely to fail spectacularly in a 64-bit setting ...

      --
      Eric.Sosman@sun .com

      Comment

      • Bartc

        #4
        Re: Function pointer cast


        "Eric Sosman" <Eric.Sosman@su n.comwrote in message
        news:1208464501 .259090@news1nw k...
        Bartc wrote:
        >I have a variable like this:
        >>
        >int *p;
        >>
        >Sometimes this points to an int location containing the address of a
        >void(void) function.
        >>
        >How can I cast it so that I can call that function?
        >>
        >I've tried casting to void **(void). The compiler keeps saying such a
        >cast is illegal.
        The call would look like
        >
        ((void(*)(void) )*p)();
        >
        ... which might be more readable with a typedef (I dislike
        typedefs that name object pointer types, but for function
        pointers they are often helpful):
        That works perfectly, thanks!

        I don't care what it looks like, it's only used in one place; in context it
        looks like this (part of byte-code opcode dispatcher):

        stopped=0;
        do {
        ((void(*)(void) )*pcptr)();
        } while (!stopped);

        -- Bartc


        Comment

        Working...