The biggest difference between the 32-bit and the 64-bit compilation environments is the change in data-type models.
The C data-type model for 64-bit applications is the ILP32 data model, so named because int, long, and pointers are 32-bit data types. The LP64 data model, so named because long and pointers grow to 64-bit, is the creation of a consortium of companies across the industry. The remaining C types, int, long long, short, and char, are the same in both data-type models.
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 -m32 -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 -m64 -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 standard relationship between C integral types still holds true.
sizeof (char) <= sizeof (short) <= sizeof (int) <= sizeof (long) <= sizeof (long long)
Figure 1, Table 1, Data Type Sizes in Bits lists the basic C types and their corresponding sizes in bits in the data type models for both LP32 and LP64.
|
Current 32-bit applications typically assume that int, long, and pointers are the same size. However, the size of long and pointers changes in the LP64 data model, which can cause many ILP32 to LP64 conversion problems.
In addition, declarations and casts are very important. 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 you intend, explicitly declare the types of constants. You can also use casts in expressions to make certain that the expression is evaluated the way you intend. This practice is particularly important with sign extension, where explicit casting is essential for demonstrating intent.