On Windows in BaseTsd.h
, there are two macros: POINTER_SIGNED
and POINTER_UNSIGNED
, which resolve to __sptr
and __uptr
respectively. According to Microsoft's documentation, these are directives that specify "how the compiler converts a 32-bit pointer to a 64-bit pointer".
__sptr
, which is the default, treats the pointer as signed and converts it to 64-bit as such, so negative pointers are extended as negative, but this has the effect of incorrectly treating the most significant bit as the sign bit, which may not be desired in some cases.
Hence __uptr
was created, where the pointer is treated as unsigned instead, and is extended as unsigned. Now, I am having trouble understanding what situations can come up that would require such constructs, and how one would discern which would be most suitable.
What is the advantage of using __ptr32 instead of casting from int
or unsigned int
to a pointer and dereferencing as you would on a 64-bit platform? Both would generally invoke undefined behavior, right? How would one choose between __sptr
and __uptr
?