Beruflich Dokumente
Kultur Dokumente
Volume I, Issue 2
If, however, you must access members of compound types in this way, use the macro
FIELD_OFFSET (type, member). This macro, which is implemented in the following
code, is used in C++ to query the offsets of fields within structures and classes.
Custom Layouts
The most subtle result of the new alignment rules is that you must consider the effects of
how you write data to or read data from memory. The following function is used to se-
rialize mburst objects and writes a given list of mburst objects to a given buffer.
If we examine how this code will write or attempt to write the mburst object into the buf-
fer, we must first examine the diagram in Figure 1. Note that in the 64-bit drawing the
sizes of some fields have changed as expected. However, if you look closely at the offset
of the size field from the beginning of the structure, you will notice that this is an 8-byte
datatype that has written 4 bytes from the beginning of the buffer. As soon as you try to
write this size field to an area of memory that is not 8-byte aligned, the program will
throw an alignment fault exception at line 09.
Add your own padding to the serialized data. This resolution ensures that the
buffer is always 8-byte aligned. And while padding increases the use of space, it
eliminates alignment faults.
Have the OS handle the alignment exception. This resolution can be used if you
are writing code intended for the Itanium architecture and is executed by using the
system call SetErrorMode(SEM_NOALIGNMENTFAULTEXCEPT). This method
is the simplest but slowest way to deal with misaligned data.
Packing
Packing is the mechanism that C and C++ programmers use to
override the rules that the compiler uses to lay out structures in
memory. Be careful when using this mechanism because the com-
piler’s rules are there for a reason, and the processor does not want
these rules to be bent or broken. Therefore, the compiler will gen-
erate extra code when building the object code, inserting instruc-
tions to prevent misaligned data from causing an exception in the
processor. This extra code will affect performance, however, so
use it with care in code that is performance sensitive.
In the Microsoft® Visual C++® development system, there are several mechanisms for
modifying the packing rules used by the compiler.
/Zp. A compile-time flag that will modify the packing rules for an entire compila-
tion unit (including header files)
pack(). A compiler pragma directive that can control the packing for a section of
code
__declspec. A keyword that can be used to control the alignment of specific vari-
able declarations.
For more information about how to use these mechanisms, please see the documentation
for Visual C++.
Alignment Exceptions
Alignment exceptions can occur in any processor when data is written to or read from a
location in memory that is not appropriate for the alignment size of the data. However,
each processor may handle it in a different way.
On x86 processors, the exception never makes it to user code. Instead, when misaligned
data is read or written, the processor makes the necessary adjustments to complete the
On the IA-64 processor, the exception is always passed up to user code and causes the
application to crash if the exception is not handled.
Summary
For performance reasons, processors prefer data to be aligned to certain boundaries in
memory. Misaligned data can cause degradation in performance or even result in your
application crashing. On 64-bit processors the alignment rules change. This change can
cause either abrupt crashes in your code or performance degradation. The crashes can be
due to bugs related to assumptions in your code about the layout of memory or due to the
new alignment exceptions.
Recommended Reading
Microsoft Visual Studio technical article: Windows Data Alignment on IPF, x86, and x86-
64
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/dv_vstechart/html/vcconWindowsDataAlignmentOnIPFX86X86-64.asp