I’m trying to convert some old 32 bit code to 64 bit. One of the more frequent warnings I get is:
warning: cast of pointer to integer of different size
this happens when a function calls pthread_create which accepts a void * for passing data to the new thread. The calling thread puts in that void * an enum (hence the size mismatch in 64bit). Here’s the code:
typedef enum {
zero,
one,
two
}numbers_e;
some_function(...)
{
numbers_e mydata=zero;
...
pthread_create(..., (void *)mydata);
...
}
I managed to overcome the warning with this:
pthread_create(..., (void *)0 + my_data);
this solution is very ugly (and I’m pondering if it’s better to leave the warning as is with a big remark near the code using it). Is there another solution ?
your solution is not only ugly, it is undefined behaviour (UB) that could cause you problems in the future:
void*pointers is non standard and must be anextension of your compiler
To avoid the first you could use
(char*)0 + my_data, but this would still leave you with the second.What you could do
uintptr_t. this is a type that is guaranteed tobe compatible with
void*, if it exists. It exists on most modernplatforms. The advatage would be that your code wouldn’t compile on
platforms where it doesn’t exist, a clear indication that you’d have
to change something, then.
pthreads are designed
For both you’d have to modify the source of the called function, though, so you’d well change it to the second, the way how this should be.