a few notable omissions...
Posted May 1, 2006 23:12 UTC (Mon) by
stevenj (guest, #421)
Parent article:
Portability and Pitfalls of C-Types (developerWorks)
First, no discussion of writing portable code for large projects on Unix is complete without a mention of GNU Autoconf. (In my experience, those who dismiss autoconf usually end up reinventing it badly.) For one thing, you can't always stick with universally supported standards in this world—the author himself recommends using several C99 constructs that aren't universally implemented. Even if you do, the system to build your program is hard to make completely portable, even among Unices, without requiring users to manually edit Makefiles.
He also writes: Floating point numbers are particularly non-portable, unless all the systems involved use a common format. They might not, although a surprising number of modern systems have adopted the IEEE 754 specification. If unsure, write things out as text. First, even if two systems use IEEE 754, they may still have endian differences. Second, binary floating-point formats are essential for storing large scientific datasets, but one doesn't have to abandon portability: there are portable binary formats supported by free/libre libraries, notably NCSA's HDF format.
And then: If you are concerned about data storage requirements, give careful consideration to the possible range of values, and use the smallest type that can represent the range you care about. This advice can be a disaster for floating-point calculations (see Kahan's rules of thumb for why): unless you really know what you are doing, use double precision!
Also, Do not treat pointers as integers. While many systems tolerate some amount of abuse along these lines, some do not. Umm, intptr_t?
(
Log in to post comments)