List five guidelines that can help you in writing portable code.
Stick to the standard
1. Use ANSI/ISO standard C++
2. Instead of using vendor specific language extensions, use STL as much as possible.
Program in the mainstream Although C++ standard does not require function prototypes, one should always write them. double sqrt(); // old style acceptable by ANSI C double sqrt(double); // ANSI – the right approach
Size of data types Sizes of data types cause major portability issues as they vary from one machine to the other so one should be careful with them.
int i, j, k;
…
j = 20000;
k = 30000;
i = j + k;
// works if int is 4 bytes
// what will happen if int is 2 bytes?
Order of Evaluation As mentioned earlier during the discussion of side effects, order of evaluation varies from one implementation to other. This therefore also causes portability issues. We should therefore follow guidelines mentioned in the side effect discussion.
Arithmetic or Logical ShiftThe C/C++ language has not specified whether right shift >> is arithmetic or logical. In the arithmetic shift sign bit is copied while the logical shift fills the vacated bits with 0. This obviously reduces portability. Interestingly, Java has introduced a new operator to handle this issue. >> is used for arithmetic shift and >>> for logical shift.
1. Use ANSI/ISO standard C++
2. Instead of using vendor specific language extensions, use STL as much as possible.
Program in the mainstream Although C++ standard does not require function prototypes, one should always write them. double sqrt(); // old style acceptable by ANSI C double sqrt(double); // ANSI – the right approach
Size of data types Sizes of data types cause major portability issues as they vary from one machine to the other so one should be careful with them.
int i, j, k;
…
j = 20000;
k = 30000;
i = j + k;
// works if int is 4 bytes
// what will happen if int is 2 bytes?
Order of Evaluation As mentioned earlier during the discussion of side effects, order of evaluation varies from one implementation to other. This therefore also causes portability issues. We should therefore follow guidelines mentioned in the side effect discussion.
Arithmetic or Logical ShiftThe C/C++ language has not specified whether right shift >> is arithmetic or logical. In the arithmetic shift sign bit is copied while the logical shift fills the vacated bits with 0. This obviously reduces portability. Interestingly, Java has introduced a new operator to handle this issue. >> is used for arithmetic shift and >>> for logical shift.
0 Comments
Please add nice comments or answer ....