On the issue of program portability

Source: Internet
Author: User

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)

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.