[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; >> 2. malloc can't be used in C++ except for character arrays. I know it's deprecated anyway, but it's even ...

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 2. malloc can't be used in C++ except for character arrays. I know it's deprecated anyway, but it's even illegal? As "malloc(sizeof(SomeStruct))" won't work as the void pointer it returns may have a different width than the SomeStruct pointer?

    It's legal and perfectly safe *except* that the if it's a class-type you'd have to call the constructor and destructor manually (in fact, that's exactly what the new/delete operators do internally, although they aren't specifically required to use malloc/free, but they usually do).

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Thanks again for your answers. I understood what the problem in my way of thinking was: I didn't know about the explicit distinction to function pointers and object pointers. And then in my stupidity, I managed to miss exactly that distinction made in two separate posts.
    It also means that everything I said about malloc and pthread_create doesn't hold anymore (only that you can't malloc function pointers, which wouldn't make much sense anyway, and you can't pass a function pointer directly to the "void *arg" parameter, but that can be overcome using a struct).

    Quote Originally Posted by Sebastiani View Post
    >> 2. malloc can't be used in C++ except for character arrays. I know it's deprecated anyway, but it's even illegal? As "malloc(sizeof(SomeStruct))" won't work as the void pointer it returns may have a different width than the SomeStruct pointer?

    It's legal and perfectly safe *except* that the if it's a class-type you'd have to call the constructor and destructor manually (in fact, that's exactly what the new/delete operators do internally, although they aren't specifically required to use malloc/free, but they usually do).
    I know, again my confusion on the distinction between types of pointers. Anyways, I don't think there even is a legal way to call a constructor or destructor explicitly. I might be wrong: as clearly displayed in this post I haven't read the full standard. I read part of it, but it's just too boring to read every single rule. How the heck did you guys manager to stay awake while reading it?

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Anyways, I don't think there even is a legal way to call a constructor or destructor explicitly.
    Sure you can:

    Code:
    int main( void )
    {
    	string&
    		text = *new( malloc( sizeof( string ) ) ) string( "A string is born!" );
    	cout << text << endl;
    	text.~string( );
    	free( &text );
    	return 0;
    }

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > you can't pass a function pointer directly to the "void *arg" parameter, but that can be overcome using a struct
    or int (*fp)(void) = f; void *vp = &fp; pthread_create(thread, attr, &routine, vp);

  5. #20
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you can do it fairly easily by casting pointers-to-pointers.

    chances are, however, that any implementation that relies on this is not a very good one.

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    can you show an example of what you mean?

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by robwhit View Post
    > you can't pass a function pointer directly to the "void *arg" parameter, but that can be overcome using a struct
    or int (*fp)(void) = f; void *vp = &fp; pthread_create(thread, attr, &routine, vp);
    Well, yeah, that's possible I figured. As long as vp doesn't run out of the scope and is unchanged before the thread is actually started. So it'd probably have to be a global variable, or there has to be some synchronization method between the two.

  8. #23
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    So it'd probably have to be a global variable, or there has to be some synchronization method between the two.
    Or just allocate on the heap, and make the thread free up the memory. That's the most common way of approaching this situation.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #24
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by robwhit View Post
    can you show an example of what you mean?
    Code:
    #include <iostream>
    void f()
    {
      std::cout << "F\n";
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      void (*fp)() = &f;
      void *vp = *(void**)&fp;
      void (*_fp)() = *(void(**)())&vp;
      _fp();
    	return 0;
    }

  10. #25
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by EVOEx View Post
    Well, yeah, that's possible I figured. As long as vp doesn't run out of the scope and is unchanged before the thread is actually started. So it'd probably have to be a global variable, or there has to be some synchronization method between the two.
    well, vp is passed by value. maybe you meant fp? but it's no different than passing a pointer to anything else, like an int or a struct.

    > void *vp = *(void**)&fp;
    void** isn't a generic type. just void*. so casting a pointer to pointer to function to a void** might not work.
    Last edited by robwhit; 08-18-2009 at 11:55 AM.

  11. #26
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    that doesn't make sense.

    so long as the pointers are the same size, there shouldn't be any loss of information.

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    well you could cast &fp to a void** if you knew that fp was properly aligned for a void*, but you don't know that (or do you?).

  13. #28
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    so long as function-pointers are aligned on a equal-or greater multiple of the same boundary that's not a problem, is it?

  14. #29
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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.

    >> 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). 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.

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by Sebastiani
    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.
    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.
    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

Page 2 of 3 FirstFirst 123 LastLast
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, 11:10 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. why typedef? and not a pointer to function?
    By terracota in forum Windows Programming
    Replies: 10
    Last Post: 12-19-2004, 06:22 PM
  5. Glib and file manipulation
    By unixOZ in forum Linux Programming
    Replies: 1
    Last Post: 03-22-2004, 09:39 PM

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