32-bit and 64-bit machine data type length comparison

Source: Internet
Author: User

64-bit Advantage: 64-bit applications can directly access 4EB of memory and file size up to 4 EB (2 of the 63 power), access to large databases. This article describes the 64-bit 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 denotes: int type

L denotes: Long type

P indicates: pointer pointer type

32 indicates: 32-bit system

64 indicates 64-bit system

For example, LP64 says that the long type and pointer type length is 64 bits under 64-bit systems.

64-bit Linux uses the LP64 standard, which is: long and pointer type length 64 bits, other types of length and 32-bit system under the same type of length, 32-bit and 64-bit length comparison see the blue part of the figure.

The following figure shows the length of the data type detected using sizeof under 32 and 64-bit Linux systems.

32-bit platform results:

64-bit platform results:

2 64 System development Considerations

2.1 Format string: Long using%LD, the pointer uses%p, for example:

Char *ptr = &something;

printf (%x\n ", PTR);

The above code is incorrect on a 64-bit system and displays only a low 4-byte content. The correct approach is to use%p.

Char *ptr = &something;

printf (%p\n ", PTR);

2.2 Numeric constants: Constants to add L

Example 1, the constant 0xFFFFFFFF is a signed long type. On a 32-bit system, this will have all bits set (1 for each), but on 64-bit systems, only the lower 32 bits are placed, and the result is that this 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.

Long L = 1 << A;

To make a displacement on a 64-bit system, use 1L, as follows:

Long L = 1L << A;

2.3 Symbol Extensions: Avoid signed numbers and unsigned numbers, for example:

int i =-2;

unsigned int j = 1;

Long L = i + j;

printf ("Answer:%ld\n", L);

32 digits below is-1, under 64 digits is 4294967295. The reason is that the expression (I+J) is a unsigned int

expression, but the sign bit is not expanded when it is assigned to K. To solve this problem, the operands at both ends are either signed or unsigned.

2.4 Conversion truncation

Conversion truncation occurs when a long is converted to an int, as in the following example:

int length = (int) strlen (str);

Strlen returns size_t (which is unsigned long in LP64), and truncation is inevitable when assigned to an int. Typically, truncation occurs only when the length of STR is greater than 2GB, which is not normally present in the program. Even so, you should try to use the appropriate polymorphic types (such as size_t, uintptr_t, and so on).

2.5 Assignment,

Do not swap with int and long types, for example:

int i;

time_t l;

i = l;

Do not use the int type to store pointers, for example:

unsigned int i, *ptr;

i = (unsigned) ptr;

Do not use pointers to hold values of type int. For example:

int *ptr;

int i;

PTR = (int *) I;

2.6 Porting performance in 64-bit environments

After porting to the 64-bit platform, performance actually decreased. The reason is that the pointer length in 64 bits is related to the size of the data, and the resulting cache hit rate is reduced, data is aligned, 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:

The library that is linked to in the 2.7 program uses 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.