Casting pointers to functions of different types

Discussion in 'C Programming' started by Yevgen Muntyan, Oct 6, 2006.

  1. Hey,

    Is the following code valid? It has to cast a function pointer to some
    fixed type to allow api for storing and calling that function, but is
    such cast legal? Code which stored function pointer in array of unsigned
    chars would be valid (I guess), but would be lot uglier than this.

    typedef void (*AFunc) (void);
    typedef void (*ConcreteFunc) (int);

    void some_func (int)
    {
    }

    void marshal (AFunc func, int *args)
    {
    ConcreteFunc cfunc = func;
    cfunc (args[0]);
    }

    int main (void)
    {
    int arg = 1;
    AFunc func = some_func; // here we cast ConcreteFunc to AFunc
    marshal (func, &arg, 1); // and then marshal() casts it back
    }

    Thanks,
    Yevgen
    Yevgen Muntyan, Oct 6, 2006
    #1
    1. Advertising

  2. Yevgen Muntyan wrote:
    > Hey,
    >
    > Is the following code valid?


    Here's compilable code, sorry:

    typedef void (*AFunc) (void);
    typedef void (*ConcreteFunc) (int);

    void some_func (int arg)
    {
    }

    void marshal (AFunc func, int *args)
    {
    ConcreteFunc cfunc = (ConcreteFunc) func;
    cfunc (args[0]);
    }

    int main (void)
    {
    int arg = 1;
    AFunc func = (AFunc) some_func;// here we cast ConcreteFunc to AFunc
    marshal (func, &arg); // and then marshal() casts it back
    }
    Yevgen Muntyan, Oct 6, 2006
    #2
    1. Advertising

  3. Yevgen Muntyan

    Guest

    I never think this can work, you'd better check on your compiler,but I
    think 99% chance, you will receive an error message from the compiler.
    and if it is C++, you must wrong.
    If your compiler is old version, you may test a function with no
    parameter, that means, it will not check the parameter, maybe it can
    work. Good Luck!
    Yevgen Muntyan wrote:
    > Hey,
    >
    > Is the following code valid? It has to cast a function pointer to some
    > fixed type to allow api for storing and calling that function, but is
    > such cast legal? Code which stored function pointer in array of unsigned
    > chars would be valid (I guess), but would be lot uglier than this.
    >
    > typedef void (*AFunc) (void);
    > typedef void (*ConcreteFunc) (int);
    >
    > void some_func (int)
    > {
    > }
    >
    > void marshal (AFunc func, int *args)
    > {
    > ConcreteFunc cfunc = func;
    > cfunc (args[0]);
    > }
    >
    > int main (void)
    > {
    > int arg = 1;
    > AFunc func = some_func; // here we cast ConcreteFunc to AFunc
    > marshal (func, &arg, 1); // and then marshal() casts it back
    > }
    >
    > Thanks,
    > Yevgen
    , Oct 6, 2006
    #3
  4. Yevgen Muntyan wrote:
    > Hey,
    >
    > Is the following code valid?


    Nevermind, standard explicitely states it is, except errors
    not related to casts :)

    "A pointer to a function of one type may be converted to a pointer to a
    function of another type and back again; the result shall compare equal
    to the original pointer."

    Regards,
    Yevgen
    Yevgen Muntyan, Oct 6, 2006
    #4
  5. Yevgen Muntyan said:

    > Hey,
    >
    > Is the following code valid?


    No comment! :) I'm going to answer the question, if I may, rather than the
    code.

    > It has to cast a function pointer to some
    > fixed type to allow api for storing and calling that function, but is
    > such cast legal?


    Yes. All function pointer types are guaranteed to have the same
    representation, so it's okay to store any function pointer value in any
    function pointer object. BUT there are no implicit conversions, like with
    void * for objects. So unless you know the RHS is exactly the proper type
    for the LHS, you must add a cast to get it to the right type. This is
    another of those very few places where a cast is necessary and therefore
    worth having.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Oct 6, 2006
    #5
  6. On Fri, 06 Oct 2006 04:26:51 +0000, Richard Heathfield
    <> wrote:

    > Yevgen Muntyan said:


    > > It has to cast a function pointer to some
    > > fixed type to allow api for storing and calling that function, but is
    > > such cast legal?

    >
    > Yes. All function pointer types are guaranteed to have the same
    > representation, so it's okay to store any function pointer value in any
    > function pointer object. <snip>


    They aren't specifically required to have the same representation. It
    is required that you can convert any function pointer type to any
    other and back without loss of information, and I have never heard of
    or even imagined any machine where it would make sense to accomplish
    this by any means other than using the same representation.

    But what the OP was doing (or trying to) was converting, and that is
    specifically guaranteed.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, Oct 23, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mikhail Teterin
    Replies:
    9
    Views:
    356
    Richard Bos
    Dec 24, 2004
  2. Replies:
    11
    Views:
    1,401
    James Kanze
    Jun 7, 2007
  3. Gene

    Casting pointers to varargs functions

    Gene, Nov 3, 2007, in forum: C Programming
    Replies:
    6
    Views:
    341
    Eric Sosman
    Nov 3, 2007
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    672
  5. ajaybgr
    Replies:
    18
    Views:
    966
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page