Transplant guidelines From the viewpoint of software engineering, you should try to minimize those who can avoid transplantability in the code. Techniques for reducing potential transplantability have:
The size of the integer and floating point types should be regarded as the lower bound. The algorithm should be able to work well after the corresponding type of size is growing. Floating point calculations should be able to use the accuracy of the variables that hold the corresponding value. The floating point algorithm should have a good operation after the accuracy of the corresponding type. Avoid depending on the order in which those side effects are calculated, because the compiler may change these sequences. For example: A B C
It can be calculated in various order: (A B) C, A (B C) B, (C B) A, etc. Parentheses control operator priority, but parentheses cannot control the order of evaluation. In particular, the function parameters may be calculated from left to right, or may be calculated from right to left, depending on the call management used. If the operand of the combined operator or * is the floating point value, the order of the expression is not adjusted. Avoid dependency order; that is, don't rely on the CPU "low byte priority" or "high byte priority". Avoid depending on the size of the pointer or reference, they may not be as sized as an integer. If inevitable is to depend on the size of the type, you should put an assert to the code for verification: assert (int.sizeof == (int *). Sizeof);
The 32-bit platform transplays 64-bit processors and operating systems to us. Preliminary ideas:
Whether at 32 or in the 64-bit code, the size of the integer type is the same. After 32-bit migrated to 64-bit, the size of the pointer and reference will increase from 4 bytes to 8 bytes. Use SIZE_T as an alias that can overwrite the unsigned integer type of the entire address space. Use PTRDIFF_T as an alias that can overwrite the unsigned integer type of the entire address space. .length, .size, .sizeof and .alignof attribute type is SIZE_T.
The OS-specific code is specific to the system's code should be separately placed in separate modules. Import a module specific to the current system upon compile time.
When processing a small difference, a constant can be defined within a system-specific module, and then use the IF statement to process various situations.