Skip to main content
gamedev.net
🔒 Locked

passing array to thread

Started by icecubeflower Dec 6, 2008 at 12:44 PM 3 replies 1.1k views
Original Post
icecubeflower
icecubeflower
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.
rip-off
rip-off
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);// orstatic_cast<int *>(data)[j] = static_cast<int *>(data);

I hope you can see why its easier to cast once at the top [smile]
icecubeflower
icecubeflower
Well, your way works.

...I really don't understand it at all though.
evillive2
evillive2
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.
Evillive2
icecubeflower
icecubeflower
Oh I get it. It's just order of operations. Duh.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.