The last part of "Linux programming" mentions the portability of the program, and here's a summary.
Portability is for compilers, operating systems, and hardware at three levels. In general, if a project has code that is specific to a system, it should be designed as a separate part.
1. From the point of view of the program, it is mainly to pay attention to the maximum value set in Limits.h.
2. From the hardware point of view, mainly attention to data length, byte order, memory alignment problems.
(1) Minimize the use of bit operations. Suppose you want to place the 2nd bit of the integer variable i, you can i=i|4; If you want to erase it's going to be a hassle. In a 32-bit machine, I&=0XFFFB is OK. But if it is a 64-bit machine, the first 32 bits of I will be erased.
(2) byte order: Try not to take the function parameter to address operation. Also pay attention to the characteristics of Union
Func (Char zz)
{
Char *p, temp;
p=&zz; /*not a good idea*/
Temp=zz;
p=&temp; /*much safer*/
}
3. From the compiler's point of view, some compilers are scanned from left to right, while others are reversed. So the following code is not good:
Func (a++, B=a+c)