casting function pointers ?

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

    #1

    casting function pointers ?

    I have come accross some code where function pointers are being cast
    from one type to another (admittedly, they all have the same return
    type). eg. you may have func ptrs declr as ff:

    typedef void (*vfptr)(void) ;
    typedef void (*vfptr_i)(int) ;
    typedef void (*vfptr_ii)(int , int ) ;
    typedef void (*vfptr_iis)(in t, int, char*) ;

    the first declaration is used like a kind of base class functor in the
    sense that sometimes, the other classes are cast "down" to it. In these
    cases, the func args are determined at run time - typically, retrieved
    from global variables etc.

    This seems quite reckless. I have never seen anything like it before.
    Has anyone come accros something like this before?

  • Giorgos Keramidas

    #2
    Re: casting function pointers ?

    Alfonso Morra <sweet-science@the-ring.com> writes:[color=blue]
    > I have come accross some code where function pointers are being cast
    > from one type to another (admittedly, they all have the same return
    > type). eg. you may have func ptrs declr as ff:
    >
    > typedef void (*vfptr)(void) ;
    > typedef void (*vfptr_i)(int) ;
    > typedef void (*vfptr_ii)(int , int ) ;
    > typedef void (*vfptr_iis)(in t, int, char*) ;
    >
    > the first declaration is used like a kind of base class functor in the
    > sense that sometimes, the other classes are cast "down" to it. In these
    > cases, the func args are determined at run time - typically, retrieved
    > from global variables etc.
    >
    > This seems quite reckless. I have never seen anything like it
    > before. Has anyone come accros something like this before?[/color]

    I guess whoever wrote these casts doesn't care too much about weird
    stack problems :-)

    Comment

    • Eric Sosman

      #3
      Re: casting function pointers ?

      Alfonso Morra wrote:
      [color=blue]
      > I have come accross some code where function pointers are being cast
      > from one type to another (admittedly, they all have the same return
      > type). eg. you may have func ptrs declr as ff:
      >
      > typedef void (*vfptr)(void) ;
      > typedef void (*vfptr_i)(int) ;
      > typedef void (*vfptr_ii)(int , int ) ;
      > typedef void (*vfptr_iis)(in t, int, char*) ;
      >
      > the first declaration is used like a kind of base class functor in the
      > sense that sometimes, the other classes are cast "down" to it. In these
      > cases, the func args are determined at run time - typically, retrieved
      > from global variables etc.
      >
      > This seems quite reckless. I have never seen anything like it before.
      > Has anyone come accros something like this before?[/color]

      It's ugly, but far from reckless and sometimes necessary.

      The Standard says that it's possible to convert a function
      pointer from one type to another without harm, so the casts
      using the types above are all right. The Standard also says,
      though, that when a call is made the type of the function
      pointer must agree with that of the called function, or else
      you get undefined behavior. So,

      void func_ii(int, int);
      vfptr fp = (vfptr)func_ii; /* all is well */
      fp(1, 2); /* compile-time error */
      fp(); /* undefined behavior */
      ((vfptr_ii)fp)( 1, 2); /* all is well */

      You usually find this sort of thing in connection with a
      table of function pointers accompanied by type codes of some
      kind. They'll be used more or less like this

      vfptr fp = table[i].funcptr;
      switch (table[i].functype) {
      case V:
      fp();
      break;
      case I:
      ((vfptr_i)fp)(1 );
      break;
      case II:
      ((vfptr_ii)fp)( 1, 2);
      break;
      case IIS:
      ((vfptr_iis)fp) (1, 2, "three");
      break;
      ...
      default:
      fprintf (stderr, "BUG: unknown type code %d\n",
      table[i].functype);
      die_horribly();
      }

      .... to allow the program to make a run-time choice among many
      functions with different "signatures ."

      --
      Eric Sosman
      esosman@acm-dot-org.invalid

      Comment

      • Alfonso Morra

        #4
        Re: casting function pointers ?



        Eric Sosman wrote:[color=blue]
        > Alfonso Morra wrote:
        >[color=green]
        >> I have come accross some code where function pointers are being cast
        >> from one type to another (admittedly, they all have the same return
        >> type). eg. you may have func ptrs declr as ff:
        >>
        >> typedef void (*vfptr)(void) ;
        >> typedef void (*vfptr_i)(int) ;
        >> typedef void (*vfptr_ii)(int , int ) ;
        >> typedef void (*vfptr_iis)(in t, int, char*) ;
        >>
        >> the first declaration is used like a kind of base class functor in the
        >> sense that sometimes, the other classes are cast "down" to it. In
        >> these cases, the func args are determined at run time - typically,
        >> retrieved from global variables etc.
        >>
        >> This seems quite reckless. I have never seen anything like it before.
        >> Has anyone come accros something like this before?[/color]
        >
        >
        > It's ugly, but far from reckless and sometimes necessary.
        >
        > The Standard says that it's possible to convert a function
        > pointer from one type to another without harm, so the casts
        > using the types above are all right. The Standard also says,
        > though, that when a call is made the type of the function
        > pointer must agree with that of the called function, or else
        > you get undefined behavior. So,
        >
        > void func_ii(int, int);
        > vfptr fp = (vfptr)func_ii; /* all is well */
        > fp(1, 2); /* compile-time error */
        > fp(); /* undefined behavior */
        > ((vfptr_ii)fp)( 1, 2); /* all is well */
        >
        > You usually find this sort of thing in connection with a
        > table of function pointers accompanied by type codes of some
        > kind. They'll be used more or less like this
        >
        > vfptr fp = table[i].funcptr;
        > switch (table[i].functype) {
        > case V:
        > fp();
        > break;
        > case I:
        > ((vfptr_i)fp)(1 );
        > break;
        > case II:
        > ((vfptr_ii)fp)( 1, 2);
        > break;
        > case IIS:
        > ((vfptr_iis)fp) (1, 2, "three");
        > break;
        > ...
        > default:
        > fprintf (stderr, "BUG: unknown type code %d\n",
        > table[i].functype);
        > die_horribly();
        > }
        >
        > ... to allow the program to make a run-time choice among many
        > functions with different "signatures ."
        >[/color]


        Thanks for the clarification - this sure is an eye opener.

        Comment

        • Barry Schwarz

          #5
          Re: casting function pointers ?

          On Fri, 29 Jul 2005 14:41:38 +0000 (UTC), Alfonso Morra
          <sweet-science@the-ring.com> wrote:

          snip 70 + lines
          [color=blue]
          >Thanks for the clarification - this sure is an eye opener.[/color]

          Please trim your posts. You do not need to quote everything just to
          say thanks.


          <<Remove the del for email>>

          Comment

          Working...