portability" -- Definition" "
Portability means that code can be
recompiled and run under different computing environments
without modification. Although true portability is an ideal
that is difficult to realize, you can take a number of
practical steps to ensure that your code is portable:
- +o
- Do not assume that an integer and a pointer have the
same size. Remember that undeclared functions are assumed
to return an int. If a function returns a
pointer, declare it so.
- +o
- Do not write routines that depend on a particular order
of code evaluation, particular byte ordering, or particular
length of data types.
- +o
- Do not write routines that play tricks with a machine's
``magic characters''; for example, writing a routine that
depends on a file's ending with &&lltt;;ccttrrll--ZZ&&ggtt;; instead of
EEOOFF ensures that that code can run only under operating
systems that recognize this magic character.
- +o
- Always use manifest constants, such as EEOOFF, and make
full use of ##ddeeffiinnee statements.
- +o
- Use header files to hold all machine-dependent
declarations and definitions.
- +o
- Declare everything explicitly. In particular, be sure
to declare functions as vvooiidd if they do not return a value;
this avoids unforeseen problems with undefined return
values.
- +o
- Do not assume that integers and pointers have the same
size or even the same kind of structure. Do not assume that
pointers are all the same or can point anywhere. On the
i8086, in SMALL model a pointer to a function addresses
relative to the code segment, whereas a pointer to data
addresses relative to the data segment. On some machines,
character pointers are of a different size or structure than
word pointers.
- +o
- The constant NULL is defined as being different from
any valid pointer. Use it and nothing else for that
purpose.
- +o
- Keep test scripts, preferably at the function level.
That is, follow each function with an
#ifdef TEST
section that will exercise that function. Running these can
rapidly isolate portability problems.
- +o
- Place plenty of
#assert
statements in your programs. These can often pick up
portability problems.
See Also