[33.8] Can I convert a pointer-to-function to a void*?

This is a discussion on [33.8] Can I convert a pointer-to-function to a void*? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Sebastiani >> so long as function-pointers are aligned on a equal-or greater multiple of the same boundary ...

  1. #31
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by Sebastiani View Post
    >> so long as function-pointers are aligned on a equal-or greater multiple of the same boundary that's not a problem, is it?

    I think they're guaranteed to be compatible. At any rate, it isn't necessary - just cast the function to (void*) and be done with it.
    yes but you cannot do that with a method pointer.

    Code:
    class C
    {
      public:
      void f()
      {
        std::cout << "F\n";
      }
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      void (C::*fp)() = &C::f;
      void *vp = *(void**)&fp;
      void (C::*_fp)() = *(void (C::**)())&vp;
      C c;
      (c.*_fp)();
    	return 0;
    }

    i'm not advocating this as a good idea by any means, just that it always "seems to work".

    imo abstract functors are a much much better way to accomplish something like this.

  2. #32
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    I think that m37h0d's method relies on something implementation defined, or even undefined behaviour. Just casting the function pointer to void* is illegal, as has been pointed out in this thread.
    For member functions, yes - but ordinary functions?

    Code:
    void func( int data )
    {
        cout << data << endl;
    }
    
    typedef void ( * fptr )( int );
    
    void call( void* vptr, int data )
    {
        ( ( fptr )vptr )( data );
    }
    
    int main( void )
    {
        call( ( void* )func, 1024 );
    }

  3. #33
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by m37h0d View Post
    yes but you cannot do that with a method pointer.

    Code:
    class C
    {
      public:
      void f()
      {
        std::cout << "F\n";
      }
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      void (C::*fp)() = &C::f;
      void *vp = *(void**)&fp;
      void (C::*_fp)() = *(void (C::**)())&vp;
      C c;
      (c.*_fp)();
        return 0;
    }
    i'm not advocating this as a good idea by any means, just that it always "seems to work".

    imo abstract functors are a much much better way to accomplish something like this.
    Ah, I see, well I thought we had already covered that, and it seemed everyone was talking about normal functions. Anyway, the example you posted is definitely not portable/safe. I can't remember the exact reason, but I believe it has something to do with the fact that the implementations is allowed to use more than one pointer to represent a pointer to a member function (vtables and whatnot, I think).

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Sebastiani
    For member functions, yes - but ordinary functions?
    The conversion of a pointer to a function to a pointer to an object or void is not defined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #35
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Sebastiani View Post
    Nope, pointers don't go out of scope, just what they point to (and functions never go out of scope). And even if you changed the pointer after the thread creation but before thread execution, it would still work, since the thread would have a copy of the original pointer.
    Sorry, robwhit was right, I meant fp there. So where the pointer the pointer points to runs out of scope. I'm having a lot of brainfarts the last few days .

  6. #36
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    The conversion of a pointer to a function to a pointer to an object or void is not defined.
    I see. Well, it is hard to see how that wouldn't work, though. Anyway, thanks for pointing that out.

  7. #37
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by EVOEx View Post
    Sorry, robwhit was right, I meant fp there. So where the pointer the pointer points to runs out of scope. I'm having a lot of brainfarts the last few days .
    You and me both.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Sebastiani
    Well, it is hard to see how that wouldn't work, though.
    Well, robwhit did mention something about:
    Quote Originally Posted by robwhit
    you can't convert pointers to functions to void* because void* is only for object (aka data) types. I think it was made like that because of harvard architecture processors, where code is on one bus and data is on another bus. the two buses could have different address widths.
    But I am not sure if that is the correct explanation, and even in such a case one can probably get away with just casting to and fro.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by laserlight View Post
    Well, robwhit did mention something about:

    But I am not sure if that is the correct explanation, and even in such a case one can probably get away with just casting to and fro.
    Right, one of those "just in case" features. Oh well, the workaround isn't too difficult, anyway. Besides, there are much better ways to go about it in C++, so it's really no loss.

  10. #40
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by m37h0d View Post
    so long as function-pointers are aligned on a equal-or greater multiple of the same boundary that's not a problem, is it?
    right.
    Quote Originally Posted by Sebastiani View Post
    >> As long as vp doesn't run out of the scope and is unchanged before the thread is actually started.

    Nope, pointers don't go out of scope, just what they point to (and functions never go out of scope).
    Code:
    void f(void) {
        int *p, f2(void);
    } /* pointer p and function f2 go out of scope */
    
    int f2(void) {
        return 0;
    }
    I made an error in post #10, sizeof [v|i|c] should be sizeof *[v|i|c].

  11. #41
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by Sebastiani View Post
    Ah, I see, well I thought we had already covered that, and it seemed everyone was talking about normal functions. Anyway, the example you posted is definitely not portable/safe. I can't remember the exact reason, but I believe it has something to do with the fact that the implementations is allowed to use more than one pointer to represent a pointer to a member function (vtables and whatnot, I think).
    IIRC, that information is encoded into the pointer, and those considerations are why member functions are not simple offsets from the address of the class instance.

    i looked into this (a little) a few months ago when i was doing some serialization work; it seemed that it would be handy to allow for a setter member function to be encoded as a handler with the serialized data. in the end, i decided this was thoroughly evil and i used a boost-esque implementation.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. why typedef? and not a pointer to function?
    By terracota in forum Windows Programming
    Replies: 10
    Last Post: 12-19-2004, 05:22 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 08:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21