Typecasting function pointers to void *

Discussion in 'C Programming' started by bnoordhuis@gmail.com, Jul 15, 2005.

  1. Guest

    Consider this:

    int foo(int *a, int *b);
    int (*bar)(void *, void *) = (void *)foo;

    How legal - or illegal - is the typecast and are there real-world
    situations where such code will cause trouble? I don't mean trouble
    like in 'not compiling' but trouble like in 'crashing hard'.
    , Jul 15, 2005
    #1
    1. Advertising

  2. pete Guest

    wrote:
    >
    > Consider this:
    >
    > int foo(int *a, int *b);


    > (void *)foo;


    > How legal - or illegal - is the typecast and are there real-world
    > situations where such code will cause trouble? I don't mean trouble
    > like in 'not compiling' but trouble like in 'crashing hard'.


    Converting a pointer to a function,
    to a pointer to type void, is undefined.

    --
    pete
    pete, Jul 15, 2005
    #2
    1. Advertising

  3. In article <>,
    <> wrote:
    >Consider this:
    >
    >int foo(int *a, int *b);
    >int (*bar)(void *, void *) = (void *)foo;
    >
    >How legal - or illegal - is the typecast and are there real-world
    >situations where such code will cause trouble? I don't mean trouble
    >like in 'not compiling' but trouble like in 'crashing hard'.


    Conversions between function pointers and object pointers (void * is a
    special case of "object pointers") is undefined.
    I'd expect to see problems on systems with segmented memory spaces -
    code and data segments are the first obvious way of using features like
    segmentation, and the conversions between pointers into the different
    segments may or may not be lossy (and there's no requirement that the
    implementation fake lossless conversions if the underlying conversion
    is lossy like there would be with void * and object pointers).

    Any function pointer can be converted into any other function pointer
    type and back to its original type without losing necessary information,
    so if you need a "generic function pointer type" casting to something like
    void (*)(void) and back will work. Note that this doesn't have the "able
    to convert without a cast" feature that void * has for object pointers.


    dave

    --
    Dave Vandervies
    There's a certain glee I get in the cockles of my heart every time I'm
    reminded that there is no Flash plugin for my current platform.
    --Eric Schwartz in the scary devil monastery
    Dave Vandervies, Jul 15, 2005
    #3
  4. wrote on 15/07/05 :
    > Consider this:
    >
    > int foo(int *a, int *b);
    > int (*bar)(void *, void *) = (void *)foo;
    >
    > How legal - or illegal - is the typecast and are there real-world
    > situations where such code will cause trouble? I don't mean trouble
    > like in 'not compiling' but trouble like in 'crashing hard'.


    The behaviour is undefined.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "There are 10 types of people in the world today;
    those that understand binary, and those that dont."
    Emmanuel Delahaye, Jul 15, 2005
    #4
    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. Ollej Reemt
    Replies:
    7
    Views:
    526
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    788
    The Real OS/2 Guy
    Oct 28, 2003
  3. Peter Goddard

    void pointers & void function pointers

    Peter Goddard, May 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    514
    Peter Goddard
    May 16, 2005
  4. Replies:
    5
    Views:
    830
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    406
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page