#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    32
    Rep Power
    12

    Unhappy invalid conversion from `void*' to `void*(*)(void*)


    Hi everyone,

    I am trying to implement a threaded program in C++ and get this error when i compile the program

    Code:
    invalid conversion from `void*' to `void*(*)(void*)
    The code just compiles fine in C and when i compile the same program with g++ it gives the above error. I read about this problem in the usenet group comp.lang.c++ but the threads were way too long to follow and comprehend what they were saying abt this.

    My code is :

    Code:
    #include <stdio.h>
    #include <pthread.h>
    
    void print_message_function( void *ptr );
    void main_thread ( void *ptr );
    
    main()
    {
         pthread_t thread1, thread2;
         int  iret1, iret2;
    
         iret1 = pthread_create (&thread1, NULL, (void *)&main_thread, NULL);
    
         pthread_join( thread1, NULL);
         printf("Thread 1 returns: %d\n",iret1);
    }
    
    void main_thread ( void *ptr )
    {
      char *message1 = "Thread 1";
      char *message2 = "Thread 2";
      pthread_t thread3, thread4;
      int iret3, iret4;
    
      iret3 = pthread_create(&thread3, NULL, (void *)&print_message_function, (void *) message1);
      iret4 = pthread_create(&thread4, NULL, (void *)&print_message_function, (void *) message2);
    
    }
    
    void print_message_function( void *ptr )
    {
         char *message;
         message = (char *) ptr;
         printf("%s \n", message);
    }
    So wherever i used void * the compiler gives me a warning. Any suggestions would be really helpful.

    Karthik
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    I think pthread_create expects its third argument to have type void *(*)(void *), but you are passing it something of type void * (by casting). The compiler complains because a void * can't implicitly be converted to a void *(*)(void *).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    32
    Rep Power
    12
    Exactly, the syntax of pthread_create inside pthread.h is:

    Code:
    extern int pthread_create (pthread_t *__restrict __threadp,
                               __const pthread_attr_t *__restrict __attr,
                               void *(*__start_routine) (void *),
                               void *__restrict __arg) __THROW;
    In my code the above line translates to:

    Code:
    int pthread_create (pthread_t &thread3,
                                   NULL,
                                   (void *) &print_message_function,
                                   void * message1) __THROW;
    So i dont see how it is different from the syntax, do i need to explicitly say that print_message_function does'nt have any arguments and put in a (void *) in there ?? , As i said before the code compiles fine with gcc and has problems with g++. I read in the usenet groups about differences in the way void * are handled by g++. I did'nt quiet understand it and the thread had like 100+ replies for it !! :(

    Any sugesstions would be really helpful

    Karthik
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Here's what's going on:

    (void *)&main_thread

    This expression means "take a pointer to the function 'main_thread' and convert that to a generic pointer," which isn't what you need to do. Is there a reason main_thread returns void and not void *? If it returned void *, it wouldn't need a cast at all, since it would have the correct type. If for some reason you can't change the definition of main_thread, then the correct syntax for the cast you need is

    (void *(*)(void *))&main_thread
  8. #5
  9. Left due to despotic ad-min
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Posts
    1,044
    Rep Power
    14
    There are a couple of issues here;
    • The code compiles in C, as C allows an implicit cast from a void * to any other pointer type. C++ does not allow that implicit conversion, so the code does not compile as C++. gcc is (by default) a C compiler, while g++ is a C++ compiler.
    • pthread_create()'s third argument is the address of a function that takes a single void * argument and returns a void *. Providing it with any other function type (even if typecast to the correct type) formally yields undefined behaviour. To avoid that, it would be best to implement print_message_function() as a function of that form. In other words, have it return void * (and have it return NULL, to avoid compiler warnings). That avoids the need for any typecast at all (in C++), or relying on implicit type conversion (in C) when calling pthread_create().

    Comments on this post

    • olcayertas agrees : its really helpfull
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    32
    Rep Power
    12
    thanx for all the help, it works now. I just defined the return type of print_message_function to be (void *) and it worked just fine.

    thanx for all the help

    Karthik :)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Thank you very much


    Thanks for the answer. I have encountered the same problem and now its work.

    Comments on this post

    • salem disagrees : Should have just left the rep rather than digging up a 6 year old thread with a "me too" comment that has zero value

IMN logo majestic logo threadwatch logo seochat tools logo