Search code examples
c++visual-c++gccvisual-c++-2010visual-c++-2012

Assigning void* to UINT or unsigned int


I learned this today at my work place. And I read this, this and this before posting my question.

Here's what my senior co-worker told me:

You cannot assign void* to UINT or unsigned int. It won’t work for 64 bit.

But why? Is it because void* and unsigned int carry different sizes on different architectures (as mentioned in other questions), or something else?


Solution

  • Depends on the target for your application. You tagged VC++ and mention type UINT - thus it seems that you're building for Windows. In 32-bit Windows the pointer size is 32 bit, while in 64-bit Windows it's 64 bit. However, size of type UINT is defined similarly to 32 bit for both Windows flavors. You can use __uint64 or UINT64 MS-specific type instead of UINT to ensure it's big enough for your pointer. You can also use INT_PTR/UINT_PTR types which are specifically designed to match the size of the pointer (thus making it transparent for 32/64-bit flavors).

    See http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx for reference on various data types.

    Of course, all of these will make your program not natively portable to other architecture/OSes.