I compiled this code using Visual Studio 2010 (cl.exe /W4
) as a C file:
int main( int argc, char *argv[] )
{
unsigned __int64 a = 0x00000000FFFFFFFF;
void *orig = (void *)0xFFFFFFFF;
unsigned __int64 b = (unsigned __int64)orig;
if( a != b )
printf( " problem\ta: %016I64X\tb: %016I64X\n", a, b );
return;
}
There are no warnings and the result is:
problem a: 00000000FFFFFFFF b: FFFFFFFFFFFFFFFF
I suppose int orig = (int)0xFFFFFFFF
would be less controversial as I'm not assigning a pointer to an integer. However the result would be the same.
Can someone explain to me where in the C standard it is covered that orig
is sign extended from 0xFFFFFFFF to 0xFFFFFFFFFFFFFFFF?
I had assumed that (unsigned __int64)orig
would become 0x00000000FFFFFFFF. It appears that the conversion is first to the signed __int64 type and then it becomes unsigned?
EDIT: This question has been answered in that pointers are sign extended which is why I see this behavior in gcc and msvc. However I don't understand why when I do something like (unsigned __int64)(int)0xF0000000
it sign extends to 0xFFFFFFFFF0000000 but (unsigned __int64)0xF0000000
does not instead showing what I want which is 0x00000000F0000000.
EDIT: An answer to the above edit. The reason that (unsigned __int64)(int)0xF0000000
is sign extended is because, as noted by user R:
Conversion of a signed type (or any type) to an unsigned type always takes place via reduction modulo one plus the max value of the destination type.
And in (unsigned __int64)0xF0000000
0xF0000000 starts off as an unsigned integer type because it cannot fit in an integer type. Next that already unsigned type is converted unsigned __int64
.
So the takeaway from this for me is with a function that's returning a 32-bit or 64-bit pointer as an unsigned __int64
to compare I must first convert the 32-bit pointer in my 32-bit application to an unsigned type before promoting to unsigned __int64
. The resulting code looks like this (but, you know, better):
unsigned __int64 functionidontcontrol( char * );
unsigned __int64 x;
void *y = thisisa32bitaddress;
x = functionidontcontrol(str);
if( x != (uintptr_t)y )
EDIT again:
Here is what I found in the C99 standard:
6.3.1.3 Signed and unsigned integers
Converting a pointer to/from an integer is implementation defined.
Here is how gcc does it, i.e. it sign extends if the integer type is larger than the pointer type(this'll happen regardless of the integer being signed or unsigned, just because that's how gcc decided to implement it).
Presumably msvc behaves similar. Edit, the closest thing I can find on MSDN is this/this, suggesting that converting 32 bit pointers to 64 bit also sign extends.
From the C99 standard (§6.3.2.3/6):
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
So you'll need to find your compiler's documentation that talks about that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With