64-Bit Insider Volume 1 Issue 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

64-bit Insider

Volume I, Issue 3

The 64-bit Advantage


The computer industry is chang-
Guidelines for Writing Effective
ing, and 64-bit technology is the
next, inevitable step. The 64-bit
Polymorphic Code: Part 1
Insider newsletter will help you
adopt this technology by provid- Using Separate Code Bases
ing tips and tricks for a successful The need to create applications that target different archi-
port.
tectures is not new. Throughout the history of computing,
64-bit development and migration it has been proven that there are advantages to generat-
is not as complicated as the 16-bit ing—from a single code base—different executables that
to 32-bit transition. However, as can run on a diverse set of platforms. This approach gener-
with any new technology, there ates an application that has a competitive edge over other
are several areas that require close
examination and consideration. applications that run on a single platform or that require a
The goal of the 64-bit Insider substantial amount of work to be deployed on different ar-
newsletter is to identify potential chitectures. However, it is also important to consider the
migration issues and provide vi- various factors that have an impact on the level of effort
able, effective solutions to these required to migrate an application to a different platform.
issues. With a plethora of Web
sites already focused on 64-bit
One of the most important impacts being the portability of
technology, the intention of this the language on which the application has been written.
newsletter is not to repeat previ-
ously published information. In- This edition of the 64-bit Insider newsletter introduces poly-
stead, it will focus on 64-bit issues morphic data types, which allow you to create applications
that are somewhat isolated yet written in C or C++ that, from a single source code, can be
extremely important to under-
stand. It will also connect you to compiled to run on x86, x64, and Itanium processors.
reports and findings from 64-bit
experts.

64-bit Insider Newsletter


Volume 1, Issue 3
Page 1/4
Introducing Polymorphic Data Types
Polymorphic data types consist of several new pointer-
precision types that have a different size depending on
the program’s target architecture. This size matches that
of the pointer in the architecture to which the
application is to be compiled.

For example, consider the INT_PTR type. If this type is


being compiled for a 32-bit processor, then it will be
4 bytes in size. However, if the target platform is a 64-
bit processor, then the size of INT_PTR type will be 8 bytes.

The polymorphism on the INT_PTR type is defined in BaseTsd.h, as follows.


#if defined(_WIN64)
typedef __int64 INT_PTR;
#else
typedef int INT_PTR;
#endif

The Importance of Polymorphic Data Types


With the introduction of the 64-bit version of Microsoft Windows operating system
comes a need to deploy applications that target various platforms. In some cases, this ap-
proach requires applications to be built from scratch. However, many existing 32-bit ap-
plications can be migrated to the 64-bit platform.

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.

64-bit Insider Newsletter


Volume 1, Issue 3
Page 2/4
However, 64-bit Windows uses the LLP64 data model (also known as the P64 data
model), the name of which indicates that only pointers have changed size to 64 bits.

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.

Using Polymorphic Data Types


The first and most important rule to follow when typecasting pointers in 64-bit Windows
is to only cast between compatible types. In a 32-bit application pointer types are com-
patible with the integer types int, long, ULONG, DWORD, and many others. In a 64-bit
application this is no longer true. If you run 32-bit and 64-bit versions of your application
from the same source code, unexpected results will show up at runtime in your 64-bit ap-
plication because of pointer truncation problems.

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.

Line 118: void *newPtr = (void *)((int)ptrToCast);

When you compile code using the /Wp64 flag, the compiler reports each possible port-
ability error.

pointertruncation.cpp(118) : warning C4311: 'type cast' : pointer

truncation from 'void *' to 'int'

pointertruncation.cpp(118) : warning C4312: 'type cast' : conversion from 'int' to 'void *' of greater size

64-bit Insider Newsletter


Volume 1, Issue 3
Page 3/4
Note Please remember that the /Wp64 flag can be used for both 32-bit and 64-bit compilers. The warnings dis-
played for both compilers will be the same.

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.

In addition, after ptrToCast has been


typecast to int, it will be typecast again
to a void pointer. In this case a 4-byte
int will be converted to an 8-byte
pointer and the upper 32 bits will be
meaningless.

To avoid truncation, it is necessary to


perform all castings to types that have
the same size.

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

Tricks for Porting Applications to 64-Bit Windows


http://www.devx.com/amd/Article/17783

The New Data Types


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/win64/win64/the_new_data_types.asp

64-bit Insider Newsletter


Volume 1, Issue 3
Page 4/4

You might also like