I was reading about pthreads here. In one example they had given this source code.
While creating a thread, they are passing a long type, type casted as void * type to the function !.
Inside the function they receive this value and reverse cast to get the long value.
Q1: Is it allowed to convert a pointer type to primitive data type and vice-verca (In C and C++)?
Q2. If so, is it a good thing to do this ? Shouldn’t they create a pointer to this long type, then type cast this pointer as void * and pass it on to the function.
This idea of conversion of a primitive type to pointer type I am finding very confusing ?
Conversion from any pointer type to void* in understandable, but how come primitive data type be stored in a void* type ? Isnt there a possibility that on a particular system size of a primitive type might be bigger that size allocated for pointer type ?
Q1: yes, but it is implementation defined (= platform depending) whether this will work. A version that would be a bit safer would use
uintptr_tinstead oflong.Q2: It is definitively bad style. There is not much performance penalty in doing that properly by allocating a
longand passing the address.