The main topic of this talk is related to the hardware system. For example, yourProgramYou need to support different types of CPU (x86, iSCSI, PowerPC), or the same type of CPU with different character lengths (such as x86 and x86-64), at this time you need to care about the hardware system issues.
★Size of the basic type
The size of the basic type (the number of bytes used) in C ++ changes with the length of the CPU. Therefore, if you want to represent the number of bytes occupied by an int, do not directly write"4"(By the way, directly write"4"You have also made the secret of magic number. For details, refer to here), and you should write"Sizeof (INT)"; In turn, if you want to define a sizeRequiredIt is a 4-byte signed integer. Do not use int directly. Use the predefined length type of typedef (such as int32_t of the boost library and ace_int32 of the ACE library ).
I almost forgot. The pointer size also has the above problem. Be careful.
★Byte order
If you have never heard of the byte order, see Wikipedia ". For example, on a machine with a large tail order, there is a 4-byte integer of 0x01020304, when uploaded to a machine with a small tail order through a network or file, it will become 0x04030201. It is said that there is also a machine with a middle tail Order (but I have never touched it ), the preceding integer is 0x02010403.
If your application involves network communication, you must remember to translate the host sequence and network sequence. If the application involves transferring binary files across machines, remember to perform similar conversions.
★Memory alignment
If you do not know what memory alignment is, see Wikipedia ". Simply put, for the sake of CPU processing performance, the data in the struct is not close to each other, but to open some gaps. In this case, the address of each data in the struct is exactly an integer multiple of the length of a specific character.
Since the C ++ standard does not define the details of memory alignment, yourCodeIt cannot depend on alignment details. Sizeof () is used to calculate the size of a struct ().
Some compilers support # pragma pack pre-processing statements (which can be used to modify the alignment length). However, this syntax is not supported by all compilers and should be used with caution.
★Shift operation
For shifts to the right of signed integers, some systems use shift to the right by default (the highest sign bit remains unchanged), and some use shift to the right by default (the highest sign bit fills 0 ). Therefore, do not shift the right of signed integers. By the way, even if there is no portability problem, the shift operator should be used as few as possible in the code. ThoseAttemptMore attention should be paid to the use of shift operations to improve performance. This operation is not only very readable, but also thankless. As long as the compiler is not mentally retarded, it will automatically help you with this optimization, without the need for programmers to worry about it.
Next post, I want to talk about "cross-platform questions related to the operating system ".
Http://program-think.blogspot.com/2009/01/cxx-cross-platform-develop-4-hardware.html