Sign in to follow this  

passing array to thread

This topic is 3290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I write a function for a threaed and pass an array of chars, it works fine:
int permutify(void *data)
{
   char rgcval[50];
   strcpy(rgcval,(char*)data)

   //cut out lots of code
}

I call the above function in main() like this: scramble_thread=SDL_CreateThread(permutify, rgcscrambler); But when I try to use an integer array it won't compile. I do this:
int permutify(void *data)
{
   int istore;
   //cut out lots of code

   istore=(int*)data[j];         //this is lines 49, 50, and 51
   (int*)data[j]=(int*)data[i];
   (int*)data[i]=istore;
   
   //cut out lots of code
}

Now it won't compile. It says:
/home/icecube/zodiac2/src/permute.cpp: In function 'int permutify(void*)':
/home/icecube/zodiac2/src/permute.cpp:49: error: pointer of type 'void *' used in arithmetic
/home/icecube/zodiac2/src/permute.cpp:49: error: 'void*' is not a pointer-to-object type
/home/icecube/zodiac2/src/permute.cpp:50: error: pointer of type 'void *' used in arithmetic
/home/icecube/zodiac2/src/permute.cpp:50: error: 'void*' is not a pointer-to-object type
/home/icecube/zodiac2/src/permute.cpp:50: error: pointer of type 'void *' used in arithmetic
/home/icecube/zodiac2/src/permute.cpp:50: error: 'void*' is not a pointer-to-object type
/home/icecube/zodiac2/src/permute.cpp:51: error: pointer of type 'void *' used in arithmetic
/home/icecube/zodiac2/src/permute.cpp:51: error: 'void*' is not a pointer-to-object type
gmake[2]: *** [permute.o] Error 1
gmake[2]: Target `all' not remade because of errors.

I think it just doesn't like me trying to access elements of a void*, like it won't believe me when I tell it it's an array or something. I don't know.

Share this post


Link to post
Share on other sites
Cast once:

int permutify(void *param)
{
int *data = static_cast<int *>(param);

int istore;
//cut out lots of code

istore = data[j]; //this is lines 49, 50, and 51
data[j] = data[i];
data[i] = istore;

//cut out lots of code
}

Your problem is that the cast applies after the rest of the expression is evaluated. You would need:

((int*)data)[j] = ((int*)data)[i];

// or

static_cast<int *>(data)[j] = static_cast<int *>(data)[i];

I hope you can see why its easier to cast once at the top [smile]

Share this post


Link to post
Share on other sites
Your problem seems to stem from a mis-understanding of how casting works and specifically the order of operations involved. Notice the parenthesis rip-off used and how it forces the cast to apply to the variable before the array subscript (i.e. pointer arithmetic) is evaluated. Your original usage makes no such clarification on the order of operations and the compiler complains because it doesn't know how to do pointer arithmetic on a void pointer (or at least knows it is probably a mistake to do so).

In addition to researching casting in general I would also recommend doing a little research specifically on how casting works in C++ as opposed to C.

Share this post


Link to post
Share on other sites

This topic is 3290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this