Writing 64 bit Clean code
Data Type 32 64 char 1 byte 1 byte short 2 bytes 2 bytes int 4 bytes 4 bytes long 4 bytes 8 bytes pointer 4 bytes 8 bytes size_t 4 bytes 8 bytes long long 8 bytes 8 bytes
Notes:
• The size and alignment of long integers and pointers have changed
from 32 bit to 64 bit.
• For the most part, as long as your code always uses the sizeof
function when allocating data structures, the size and alignment of
pointers should not affect your code, because structures containing
pointer members are generally not written to disk or sent across
networks between 32-bit and 64-bit applications. However, the
change in the size of long can cause problems if you frequently
move data between variables of type int and long.
• You should generally avoid casting a pointer to a non-pointer type
for any reason (and particularly not for performing address
arithmetic). If possible, rewrite any code that does this, either by
changing the data types or by replacing address arithmetic with
pointer arithmetic.
• If you are shifting through the bits stored in a variable of type long,
1 Depends on whether _FILE_OFFSET_BITS=64
you should be careful to avoid assuming that it is of a particular
length. Instead, use the value sysconf(_SC_LONG_BIT) to
determine the number of bits in a long.
• Be careful when using bit masks with variables of type long,
because the width will change between 32-bit and 64-bit
architectures.
• If you want the mask value to contain zeroes in the upper 32 bits on
a 64-bit architecture, the usual fixed-width mask will work as
expected, because it will be extended in an unsigned fashion to a
64-bit quantity.
• Use explicit types where possible. For example, types with names
like sint32_t and uint32_t will always be a 32-bit quantity,
regardless of future architectural changes.
• Conversion of shorter types to 64-bit longs may yield unexpected
results in certain cases. Be sure to read the section “Sign Extension
Rules for C” if you are seeing unexpected values from math that
mixes int and long variables.
• The alignment of long long (64-bit) integers has changed from 32
bit to 64 bit. This can pose a problem when you are exchanging
data between 32-bit and 64-bit applications.
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]