Below is a list of assumptions which are not necessarily safe to make, and some assumptions which are safe.
int
.long
, float
, double
, or long double
can be at any even address.unsigned
.struct
or a class
is laid out in memory, or that they can be written to a data file ‘as is’.void*
if you want an untyped pointer, not char*
. Pointers to functions might have a different size than void*
.offsetof
only works for PODs (Plain Old Data types).ANSI specifies the following about C’s built in types. This is all you can safely assume:
unsigned char
can hold at least 0 to 255; it may hold more.signed char
can hold -127 to +127; it may hold more.char
may be either an unsigned char
or a signed char
. You cannot assume either. Therefore, do not use char
(without a prefix) unless you do not care about sign extension.short
can hold at least -32,767 to 32,767 (signed
) or 0 to 65,535 (unsigned
).long
can hold at least -2,147,483,647 to 2,147,483,647 (signed
) or 0 to 4,294,967,295 (unsigned
).int
holds at least 16 bits and therefore can hold at least -32,767 to 32,767 (signed
) or 0 to 65,535 (unsigned
). In other words, an int
cannot be counted on to hold any more than a short
. int
is an appropriate type to use when a short
would be large enough but you would like to use the processor’s “natural” word size to improve efficiency—on some machines, a 32-bit operation is more efficient than a 16-bit operation because there is no need to do masking. If you need values larger than a short
can hold, you must specify long
.For more information, see section 4.6 of [Stroustrup1997] and Item 91 of [Sutter2005a].