@ Clear off all types of 32-bit and 64-bit 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 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?
    1. Char *ptr = &something;
    2. 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?
    1. Char *ptr = &something;
    2. 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?
    1. Long L = 1 << A;

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

[CPP]View Plaincopyprint?
    1. Long L = 1L << A;

2.3 Symbol Extension: Avoid signed and unsigned number operations, for example:

[CPP]View Plaincopyprint?
    1. int i =-2;
    2. unsigned int j = 1;
    3. Long L = i + j;
    4. 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?
    1. 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?
    1. int i;
    2. time_t l;
    3. i = l;

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

[CPP]View Plaincopyprint?
    1. unsigned int i, *ptr;
    2. i = (unsigned) ptr;

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

[CPP]View Plaincopyprint?
    1. int *ptr;
    2. int i;
    3. 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

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.