As stated previously, the biggest difference between the 32-bit environment and 64-bit environment is the change in two fundamental data types.
The C data-type model used for 32-bit applications is the ILP32 model, so named
because ints
, longs
, and pointers are 32-bit. The LP64 data
model is the C data-type model for 64-bit applications. This model was agreed upon
by a consortium of companies across the industry. LP64 is so named because longs and
pointers grow to 64-bit quantities. The remaining C types int
, short
, and char
are the same as in the ILP32 model.
The following sample program, foo.c, directly illustrates the effect of the LP64 data model in contrast to the ILP32 data models. The same program can be compiled as either a 32–bit program or a 64–bit program.
#include <stdio.h> int main(int argc, char *argv[]) { (void) printf("char is \t\t%lu bytes\n", sizeof (char)); (void) printf("short is \t%lu bytes\n", sizeof (short)); (void) printf("int is \t\t%lu bytes\n", sizeof (int)); (void) printf("long is \t\t%lu bytes\n", sizeof (long)); (void) printf("long long is \t\t%lu bytes\n", sizeof (long long)); (void) printf("pointer is \t%lu bytes\n", sizeof (void *)); return (0); }
The result of 32–bit compilation is:
% cc -O -o foo32 foo.c % foo32 char is 1 bytes short is 2 bytes int is 4 bytes long is 4 bytes long long is 8 bytes pointer is 4 bytes |
The result of 64–bit compilation is:
% cc -xarch=generic64 -O -o foo64 foo.c % foo64 char is 1 bytes short is 2 bytes int is 4 bytes long is 8 bytes long long is 8 bytes pointer is 8 bytes |
The default compilation environment is designed to maximize portability, that is, to create 32–bit applications.
The standard relationship between C integral types still holds true.
sizeof (char) <= sizeof (short) <= sizeof (int) <= sizeof (long)
Table 4–1 lists the basic C types, and their corresponding sizes in bits in the data type models for both LP32 and LP64.
Table 4–1 Data Type Sizes in Bits
C data type |
ILP32 |
LP64 |
---|---|---|
|
8 |
unchanged |
|
16 |
unchanged |
|
32 |
unchanged |
|
32 |
64 |
|
64 |
unchanged |
|
32 |
64 |
|
32 |
unchanged |
|
32 |
unchanged |
|
64 |
unchanged |
|
128 |
unchanged |
Some older 32-bit applications use int
, long
, and
pointer types interchangeably. The size of long
s and pointers grow in
the LP64 data model. You need to be aware that this change alone can cause many 32-bit to 64-bit conversion
problems.
In addition, declarations and casts become very important in showing what is intended. How expressions are evaluated can be affected when the types change. The effects of standard C conversion rules are influenced by the change in data-type sizes. To adequately show what is intended, you might need to declare the types of constants. Casts might also be needed in expressions to make certain that the expression is evaluated the way that you intended. Correct evaluation of expressions is particularly crucial in the case of sign extension, where explicit casting might be essential to achieve the intended effect.
Other problems arise with built-in C operators, format strings, assembly language, and compatibility and interoperability.
The rest of this chapter advises you how to overcome these problems by:
Explaining the problems outlined above in more detail
Describing some of the derived types and include files that are useful to make code safe for both 32-bit and 64-bit
Describing the tools available for helping to make code 64-bit safe
Providing general rules for making code portable between the 32-bit and 64-bit environments