void pointers & void function pointers

Discussion in 'C Programming' started by Peter Goddard, May 16, 2005.

  1. Hello,
    Is it possible, using casting to promote...

    void *Ptr;

    To behave like...

    void (*FnPtr)(void);

    ?

    i.e. Can the 'void *Ptr' be cast into calling a function once it points to
    one?

    Cheers

    Peter.
     
    Peter Goddard, May 16, 2005
    #1
    1. Advertising

  2. >Hello,
    > Is it possible, using casting to promote...
    >void *Ptr;
    >To behave like...
    >void (*FnPtr)(void);
    >?
    >i.e. Can the 'void *Ptr' be cast into calling a function once it points to
    >one?


    There is no guarantee of this. MS-DOS provides an example of having
    all possible combinations of 16-bit and 32-bit function pointers
    and 16-bit and 32-bit data pointers in the "memory models" small,
    compact, medium, and large. If you convert a 32-bit pointer to
    16-bits there is substantial risk of losing information you can't
    get back.

    Gordon L. Burditt
     
    Gordon Burditt, May 16, 2005
    #2
    1. Advertising

  3. Peter Goddard

    Eric Sosman Guest

    Peter Goddard wrote:
    > Hello,
    > Is it possible, using casting to promote...
    >
    > void *Ptr;
    >
    > To behave like...
    >
    > void (*FnPtr)(void);
    >
    > ?
    >
    > i.e. Can the 'void *Ptr' be cast into calling a function once it points to
    > one?


    No, or "not portably." Function pointers can only point
    to functions; data pointers can only point to data. If you
    try to coerce a pointer of one class to a pointer of the other,
    all bets are off -- on some implementations it will work as
    you perhaps hoped, while on others you will be unpleasantly
    surprised.

    (Usually, people who want to make this conversion have
    just filled an array with instructions and want to get the
    program to execute them. Quite aside from the difficulties
    of converting the pointer, on many machines you will face
    other obstacles. For example, a machine that "knows" the
    I- and D-spaces are distinct may well have separate cache
    mechanisms, and may not bother to synchronize them: you may
    wind up executing the garbage that occupied the array before
    you stored the instructions, which are still sitting in a
    data cache and have not yet been flushed back to memory.
    Machines that maintain "permission bits" on regions of memory
    may not set the "executable" attribute on any data memory you
    can access; you'll get some kind of trap when the CPU tries to
    fetch in instruction from a non-executable memory area. And
    so on, and so on, into endless machine-dependent territory --
    and, perhaps, an appreciation of why the C Standard chose not
    to try to enter this particular bramble bush.)

    --
     
    Eric Sosman, May 16, 2005
    #3
  4. Thanks for the replies. I had a nagging feeling that perhaps this was
    possible but not advisable.

    Cheers

    Peter.


    "Gordon Burditt" <> wrote in message
    news:...
    > >Hello,
    >> Is it possible, using casting to promote...
    >>void *Ptr;
    >>To behave like...
    >>void (*FnPtr)(void);
    >>?
    >>i.e. Can the 'void *Ptr' be cast into calling a function once it points to
    >>one?

    >
    > There is no guarantee of this. MS-DOS provides an example of having
    > all possible combinations of 16-bit and 32-bit function pointers
    > and 16-bit and 32-bit data pointers in the "memory models" small,
    > compact, medium, and large. If you convert a 32-bit pointer to
    > 16-bits there is substantial risk of losing information you can't
    > get back.
    >
    > Gordon L. Burditt
     
    Peter Goddard, May 16, 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:
    566
    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:
    812
    The Real OS/2 Guy
    Oct 28, 2003
  3. David M. Wilson
    Replies:
    8
    Views:
    494
    Ben Pfaff
    Jan 7, 2004
  4. Replies:
    5
    Views:
    850
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    418
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page