Linux 32-bit to 64-bit migration FAQs

Source: Internet
Author: User
Tags format definition
Linux 32-bit to 64-bit migration FAQ-general Linux technology-Linux programming and kernel information, the following is a detailed description. 1. Data truncation:

The operation of long type variables (Value assignment, comparison, shift, etc.) is generated. Long is defined as 32 bits on x86, and 64bits on ia64. It is easy to encounter exceptions when performing operations with int variables.

Solution: avoid calculation between different types of variables as much as possible, and avoid assigning long variables to shorter variables. unifying the variable length can solve this problem. Simply transferring 32-bit to 64-bit converts all long definitions to int definitions.

2. pointer storage:

On the x86 platform, we are used to using int to store pointers and directly participate in int-type operations. On the 64-bit platform, the pointer size is 64 bits, cannot be stored in int. Operations on pointers may also cause exceptions due to inconsistent data lengths.

Solution: Modify the variable used to store the passed pointer to the intptr_t type definition to ensure platform compatibility.

3. nonstandard structure:

An exception may occur if the specified number of Members are not named.

Solution: Name an unnamed member, declare the type symbol, and convert the long type definition to the int type.

4. Direct number in the code

An exception may occur when a value is assigned directly using a hexadecimal number (which is typically assumed to be a 32-bit variable. When the data size is defined by a number, an exception occurs during the shift operation.

Solution: Check whether the direct numbers in the Code represent various 32-bit formats, such as 4,32, 0x7fffff, and replace them with macro definitions.

Pay attention to migration during code writing:

1. Use the same header file in 32-bit and 64-bit scenarios.

2. Use a strictly prohibited format definition, such as defining the offset with off_t, defining the file location with fpos_t, and defining the pointer with intptr_t

3. Use The integer type defined in. int, long, and long are not used. Use the printf function with the integer identifier parameter. % d and % ld are not used for formatting.

4. Fixed-width or macro-defined Integer type, number, mask

5. Perform a boundary check on integer variables.

When 6.32-bit and 64-bit processes share memory operations, 64-bit is used as the operation length.
Related Article

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.