64-bit Advantage: 64-bit applications can directly access 4EB of memory and file size up to 4 EB (2 of 63 power); Access to large databases. This article describes the 64-bit under C language Development program considerations.
1.32-bit and 64-bit C data types
32 and 64-bit C language built-in data types, as shown in the following table:
The uppercase and numeric meanings of the first row in the previous table are as follows:
I means: int type
L indicates: Long type
P means: pointer pointer type
32 means: 32-bit system
64 indicates a 64-bit system
For example: LP64 indicates that long and pointer types under 64-bit systems are 64 bits long.
64-bit Linux uses the LP64 standard, that is, the long and pointer type lengths are 64 bits, the length of the other types is the same as the length of the same type under 32-bit systems, and the length of the 32-bit and 64-bit types is seen in the blue section.
The length of data types detected by sizeof for use with 32 and 64 bit Linux systems.
Results under 32-bit platform:
Results under 64-bit platform:
2.64 System Development Considerations:
2.1 Format string: Long uses%ld and the pointer uses%p, for example:
[CPP]View Plaincopyprint?
- Char *ptr = &something;
- printf (%x\n ", PTR);
The above code is incorrect on a 64-bit system and only shows content that is 4 bytes low. The correct method is: Use%p, as follows:
[CPP]View Plaincopyprint?
- Char *ptr = &something;
- printf (%p\n ", PTR);
2.2 Numeric constants: Constants to add L
Example 1, constant 0xFFFFFFFF is a signed long type. On a 32-bit system, this will place all bits (1 per bit), but on a 64-bit system, only the low 32 bits are set, and the result is that the value is 0x00000000ffffffff.
Example 2, in the following code, the maximum value of a can be 31. This is because 1 << A is of type int.
[CPP]View Plaincopyprint?
- Long L = 1 << A;
To make a displacement on a 64-bit system, use 1L as follows:
[CPP]View Plaincopyprint?
- Long L = 1L << A;
2.3 Symbol Extension: Avoid signed and unsigned number operations, for example:
[CPP]View Plaincopyprint?
- int i =-2;
- unsigned int j = 1;
- Long L = i + j;
- printf ("Answer:%ld\n", L);
32 bits under IS-1, under 64 bits is 4294967295. The reason is that the expression (I+J) is an unsigned int expression, but when it is assigned to K, the sign bit is not expanded. To solve this problem, the operands on both ends are either signed or unsigned.
2.4 Conversion Truncation:
Conversion truncation occurs when you convert a long to int, as in the following example:
[CPP]View Plaincopyprint?
- int length = (int) strlen (str);
Strlen returns size_t (which is unsigned long in LP64), which is bound to occur when assigned to an int. In general, truncation only occurs when the length of STR is greater than 2GB, which generally does not occur in a program. However, the appropriate polymorphic types (such as size_t, uintptr_t, and so on) should be used as much as possible.
2.5 Assignment:
Do not swap using int and long types, for example:
[CPP]View Plaincopyprint?
- int i;
- time_t l;
- i = l;
Do not use the int type to store pointers, for example:
[CPP]View Plaincopyprint?
- unsigned int i, *ptr;
- i = (unsigned) ptr;
Do not use pointers to hold values of type int. For example:
[CPP]View Plaincopyprint?
- int *ptr;
- int i;
- PTR = (int *) I;
2.6 Performance in porting 64-bit environments :
After porting to a 64-bit platform, performance is actually reduced. The reason is that the length of the pointer in the 64-bit is related to the size of the data, and this raises the cache hit ratio, data alignment, and so on. By changing the order in which the data is arranged in the structure, the storage space is reduced because the data is less populated. Such as:
2.7 Libraries that are linked to in the program will use a 64-bit library.
All of the problems are caused by long and pointer length changes, and only long and pointer type lengths are kept in mind during the development process.
@ Clear off all types of 32-bit and 64-bit type length comparison