int
and long
are the same size; these can vary between processors
long
, float
, double
, or long
double
can be at any even address
struct
or class is laid out in memory, or that it can be written to or read from a data file as a memory image
Pointers and integers are not interchangeable. Neither is guaranteed to hold the other.
Use
void*
if you want an untyped pointer, not char
*
. Pointer arithmetic can't be done using void*
pointers; instead, use a typed pointer (char*
for bytes).
Processor portability | ||
To illustrate portability problems, consider the MIPS R4000. This processor has 64-bit integers and 64-bit pointers. |
Future PowerPC chips will also support 64 bits. However, it's not clear whether a C compiler will make long or int 64 bits.
|
Other machines can also have large pointers (theoretically up to 48 bits on the 80386) but with 32-bit integers. |
Long double is an inherently nonportable data type and can vary in size and precision from processor to processor. It is guaranteed to hold any number a
Don't make assumptions about memory alignment because of variations between processors and compilers. Here are some common problems with alignment:
float
or double
can hold, but that's about all you can count on (for example, many RISC processors don't support any IEEE extended-precision format). Therefore, long
double
is suitable only for in-memory computations, not for data storage or network transmission. What's more, a long
double
is sometimes implemented in software and is therefore slower on some platforms.
NOTE
Avoid problems like this by ordering the data members in descending order of size to minimize alignment problems (both space and speed) on void
*
or char*
pointer is cast to a longer data type). Because most RISC processors don't support this kind of access, code that assumes the lack of restriction is not portable.
long
, float
) be on a 4-byte boundary and that 8-byte quantities (double) be on an 8-byte boundary. The compiler forces this alignment on structure elements, but if you make assumptions, you can get structures with lots of unused space.
struct
or class
on a 2-byte boundary. This is inefficient on 68020 and later processors, because the placement requires two memory accesses rather than one.
most architectures.
[Contents]
[Previous]
[Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.