Jump to content
  • Advertisement
Sign in to follow this  
icecubeflower

passing array to thread

This topic is 3598 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;
   (int*)data=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
Advertisement
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;
data = 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);

// or

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!