standards: CPU Portability
1
1 5.6 Portability between CPUs
1 ============================
1
1 Even GNU systems will differ because of differences among CPU types--for
1 example, difference in byte ordering and alignment requirements. It is
1 absolutely essential to handle these differences. However, don't make
1 any effort to cater to the possibility that an 'int' will be less than
1 32 bits. We don't support 16-bit machines in GNU.
1
1 Similarly, don't make any effort to cater to the possibility that
1 'long' will be smaller than predefined types like 'size_t'. For
1 example, the following code is ok:
1
1 printf ("size = %lu\n", (unsigned long) sizeof array);
1 printf ("diff = %ld\n", (long) (pointer2 - pointer1));
1
1 1989 Standard C requires this to work, and we know of only one
1 counterexample: 64-bit programs on Microsoft Windows. We will leave it
1 to those who want to port GNU programs to that environment to figure out
1 how to do it.
1
1 Predefined file-size types like 'off_t' are an exception: they are
1 longer than 'long' on many platforms, so code like the above won't work
1 with them. One way to print an 'off_t' value portably is to print its
1 digits yourself, one by one.
1
1 Don't assume that the address of an 'int' object is also the address
1 of its least-significant byte. This is false on big-endian machines.
1 Thus, don't make the following mistake:
1
1 int c;
1 ...
1 while ((c = getchar ()) != EOF)
1 write (file_descriptor, &c, 1);
1
1 Instead, use 'unsigned char' as follows. (The 'unsigned' is for
1 portability to unusual systems where 'char' is signed and where there is
1 integer overflow checking.)
1
1 int c;
1 while ((c = getchar ()) != EOF)
1 {
1 unsigned char u = c;
1 write (file_descriptor, &u, 1);
1 }
1
1 Avoid casting pointers to integers if you can. Such casts greatly
1 reduce portability, and in most programs they are easy to avoid. In the
1 cases where casting pointers to integers is essential--such as, a Lisp
1 interpreter which stores type information as well as an address in one
1 word--you'll have to make explicit provisions to handle different word
1 sizes. You will also need to make provision for systems in which the
1 normal range of addresses you can get from 'malloc' starts far away from
1 zero.
1