C + + portability and cross-platform development [4]: Hardware system related

Source: Internet
Author: User
Tags integer

The topic of this conversation is mainly related to the hardware system. For example, your program needs to support different types of CPUs (x86, SPARC, PowerPC), or CPUs of the same type of different word length (such as x86 and x86-64), and you need to be concerned about the hardware system.

★ The size of the base type

The size of the base type in C + + (the number of bytes occupied) varies with the length of the CPU. So, if you want to represent an int occupies the number of bytes, do not write directly "4" (by the way, directly written "4" also committed magic number of the big bogey, see here), but should write "sizeof (int)"; If you want to define a signed integer size that must be 4 bytes, and do not use int directly, use a predefined fixed-length type (such as Boost library int32_t, Ace Library Ace_int32, etc.).

Almost forgot, the size of the pointer also has the above problems, but also be careful.

★ Byte order

If you've never heard of the word "byte sequence," see "Wikipedia." In layman's parlance, there is a 4-byte integer 0x01020304 on a large-tailed machine, and it becomes 0x04030201 on a machine that passes through a network or file to a small-tailed sequence; it is said that there is a middle-tailed machine (but I have not contacted), The integers above will become 0x02010403.

If you are writing an application that involves network communication, be sure to remember to translate the host sequence and network order, and remember to do a similar conversion if you are involved in transferring binaries across machines.

★ Snap To Memory

If you don't know what "memory alignment" is, see "Wikipedia". In simple terms, the data in the structure is not next to each other for performance considerations on CPU processing, but rather to open some space. In this case, the address of each data in the structure is exactly the integer multiple of a word length.

Because the C + + standard does not define the details of the alignment of the memory, your code cannot rely on the details of the alignment. Wherever the size of the structure is calculated, the sizeof () is faithfully written.

Some compilers support #pragma pack preprocessing statements (which can be used to modify the Chi), but this syntax is not supported by all compilers and should be used with caution.

★ Shift Operation

For the right movement of signed integers, some systems use arithmetic to move to the right (the highest sign bit is the same), and some use the logical right shift (the highest symbol bit to 0). Therefore, do not move the signed integer right. By the way, even if there is no portability problem, the code will try not to use the shift operation, the readability is too poor.

Next post, get ready to talk about "operating system-related Cross-platform issues."

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.