more C to C++

Discussion in 'Mac Programming' started by farmerdoug, May 27, 2010.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    The C++ made me change all my calloc statements including.

    Code:
    
    from
    readyspline = calloc(waves, sizeof (struct formag*));
    for ( i =0; i < waves; i++)
        readyspline[i] = calloc(1, sizeof (struct formag));
    
    to
    readyspline = (formag **)calloc(waves, sizeof (struct formag*));
    for ( i =0; i < waves; i++)
        readyspline[i] = (formag*)calloc(1, sizeof (struct formag));
    
    
    now my pthread_create call
    Code:
     pthread_create(&qthread[slice], NULL, (void*)rotate, (void *)readyspline[slice]);
    
    
    is wrong. The error says Overloaded function with no contextual type information.


    help
    thanks.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I'm not that familiar with C++, but i believe NULL is void *. Maybe try casting to const pthread_attr_t * for your second argument?

    -Lee
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    As has happened before, maybe I was just lucky but I had to redefine and modify my routines.
    I made changes to mimic the example below. Have to change some more routines then I'll see if it runs.
    Code:
    
    struct thread_data{
       int  thread_id;
       int  sum;
       char *message;
    };
    
    struct thread_data thread_data_array[NUM_THREADS];
    
    void *PrintHello(void *threadarg)
    {
       struct thread_data *my_data;
       ...
       my_data = (struct thread_data *) threadarg;
       taskid = my_data->thread_id;
       sum = my_data->sum;
       hello_msg = my_data->message;
       ...
    }
    
    int main (int argc, char *argv[])
    {
       ...
       thread_data_array[t].thread_id = t;
       thread_data_array[t].sum = sum;
       thread_data_array[t].message = messages[t];
       rc = pthread_create(&threads[t], NULL, PrintHello, 
            (void *) &thread_data_array[t]);
       ...
    }
    
    thanks
     
  4. qtx43 macrumors 6502a

    Joined:
    Aug 4, 2007
    #4
    This is by design; a different design philosophy for C vs. C++. C will allow you to assign a (void*) to any other pointer type, and vice versa. C++ is much stricter about type conversions, you usually must use a cast. Although C++ will convert a derived class type to a base type automatically. And the C-style cast works, but it is preferred to use the C++ specific ones, usually static_cast<> or dynamic_cast<>. It's even better to use new or the standard library containers instead of malloc, and not do casts at all, but of course when you're interfacing with a C library sometimes you have to. For example, you can use a std:vector<std:vector <sometype> > and with an appropriate constructor and algorithm, not have to do an explicit loop at all to initialize the 2-dim array.
     
  5. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008

Share This Page