Handle Win64 Truncation Warnings

Submit New Article

November 24, 2008 11:00 PM PST



Challenge

Address Win64* compilation warnings related to truncation. Most of the warnings that you encounter when compiling for Win64 are truncation-related warnings (conversion from INT64 to int). A truncation-related warning might look something like this:

C4311: Truncation warning</b>
//For example, "typecast" pointer truncation from "int*_ptr64" to "int"

 

During the course of porting applications to the Win64 environment, it is necessary to identify and resolve compiler warnings and errors. The procedure for identifying these warnings and errors is addressed in the separate item "How to Use Makefiles to Resolve Win64 Porting Issues."

 


Solution

Cast the result to a proper data type or change the receiving variable to a scalable data type. Win64 uses the LLP64 (or P64) uniform data model, in which pointers and long long are 64 bits, but int and long are 32 bits. To support this data model and to provide compatibility with the existing Win32* code, new data types have been defined in Win64.

There are two ways to fix truncation-related warnings:

#1: Cast the result to a proper data type. Most of the non-pointer variables can be safely casted, as long as the result is guaranteed to be less than the maximum value that can be held in the casted data type. The following is a pointer-difference example of code in the error state:

char *start, *end // start and end of buffer
int x = end - start; // start/end will be 64 bits

 

Following is the modified code. Here we know that the difference (end-start) is less than 4 GB; hence it can be safely casted.

 

char *start, *end // start and end of buffer
int x = (int)(end - start); // cast the result

 

#2: Change the receiving variable to a scalable data type. By changing the receiving variables to a scalable data type, the same source code would work for 32-bit, as well as for Itanium® architecture. Scaling can cause a rippling effect where scaling a result would require several other variables and/or functions to be scaled. This will generate more warnings, which need to be addressed. The following error-state code uses variables receiving pointers:

char p
int j = p; // address will be 64 bits

 

Following is the modified code:

char p
INT_PTR j = p; // scale the result

 

The following error-state code demonstrates passing parameters into functions:

void func(int s)
............
char *p
func(p); // p will be 64 bits

 

Following is the modified code:

void func(INT_PTR s); // scale the variable
............
char *p
func(p); // p will be 64 bits

 


Source

Porting Microsoft Windows Applications to Intel® Itanium® Processor Systems