64-Bit Insider Volume 1 Issue 3
64-Bit Insider Volume 1 Issue 3
64-Bit Insider Volume 1 Issue 3
Volume I, Issue 3
When migrating an application, one of the factors that will determine the success of the
migration is whether an easy migration path has been provided. And while there are
many key points to consider when migrating an application, one of the most important
involves pointer truncation.
Pointer truncation is a plausible threat when completing a 32-bit to 64-bit migration be-
cause the pointer size in the 64-bit environment is twice as big as the pointer size in a 32-
bit environment. This size difference means that, while integer and pointer types can be
typecast from one to the other without any major complications in the 32-bit environ-
ment, such is not the case when migrating to the 64-bit
environment. When a pointer has been converted to an
integer type in the 64-bit environment, the higher 32 “... while there are many
bits of the pointer will be lost or truncated because you key points to consider when
cannot fit 64 bits into 32 bits of data. migrating an application,
one of the most important
When programming in C or C++ on 32-bit Windows, involves pointer trunca-
the types int and long, and also the pointers are all 32 tion.”
bits in size. This is known as the ILP32 (int, long,
pointer) data model.
When performing typecasts in the ILP32 model, you can mix pointers and integer types
without losing any data. However, in the LLP64 data model, typecasting from pointers to
integer types is not safe because of the differences in size. Any attempt to use a truncated
pointer can compromise the application’s stability and
raise an exception because the pointer’s address might
have been modified during the cast. “... if you want to create a
common code base, you
In typecasting situations, polymorphic data types can be must review your code and
extremely useful. Polymorphic data types provide an eliminate any pointer trun-
easy way to target both 32-bit and 64-bit platforms cation issues.”
without having to worry about pointer typecasting
problems. But to use polymorphic data types, you must
follow some important guidelines.
Therefore, if you want to create a common code base, you must review your code and
eliminate any pointer truncation issues. This procedure might seem like a daunting task.
But luckily you can use the compiler to pinpoint possible typecasting errors by adding the
/Wp64 flag as a parameter during compile time.
Let’s look at a simple example that shows pointer casting to integer types.
When you compile code using the /Wp64 flag, the compiler reports each possible port-
ability error.
pointertruncation.cpp(118) : warning C4312: 'type cast' : conversion from 'int' to 'void *' of greater size
It is important to analyze all warnings that result from using the /Wp64 flag. In this ex-
ample, there are two portability warnings on line 118. To address these warnings, you
first must understand why they occurred. The remaining discussion assumes the target is
a 64-bit platform.
On line 118, ptrToCast is a void pointer, which means that its current size is 8 bytes.
However, this pointer is being typecast to int, which is only 4 bytes in size. As a result,
pointer truncation occurs because the upper 32 bits of ptrToCast will be lost.
Upcoming Newsletter
The next issue of the 64-bit Insider
newsletter will further examine how to use polymorphic types to fix your code to be 32-
bit and 64-bit platform-compliant. In addition, Issue 4 will discuss how pointer truncation
can result in unstable code, and how you can resolve this problem.
Recommended Reading
Rules for Using Pointers
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/win64/win64/rules_for_using_pointers.asp