Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Volume I, Issue I
• Data structures change shape. Adding extra padding also causes data structures to
change shape. Member offsets change, for example, and can also lead to memory cor-
ruption.
• Writing data to memory leaves data unaligned. How you explicitly write data to
memory may no longer be allowed because the target location may leave the data un-
aligned. On Itanium processors, reading data from or writing data to inappropriate lo-
cations will cause your application to crash with a mes-
sage similar to this: “Data alignment fault.”
“... extra padding also
causes data structures to
To help locate and resolve these issues in a more practical
change shape. Member
way, the remaining sections introduce a hypothetical appli-
offsets change, for ex-
cation and associated data structure, and examine a scenario
ample, and can also lead
in which alignment problems, which were not encountered
to memory corruption...”
on a 32-bit platform, occur when the application runs on a
64-bit platform.
Sample application
Consider an application that monitors data coming from a device used in a physics labo-
ratory. This application will receive many bursts of data of varying sizes that should be
stored in a linked list for further processing. Figure 1 shows a structure that might be used
to describe the data bursts that have been detected.
mburst* create_mburst()
{
mburst* p = (mburst*) malloc(16);
p->seq_no = -1;
p->size = 0;
p->data = NULL;
p->next = NULL;
return p;
}
Because the mburst object changes in size (from 16 to 32 bits) on the 64-bit platform,
when this code runs on a 64-bit platform it will work only until you write data into the
allocated memory. At this point, the application may crash immediately, or it may con-
tinue to run for a short time and eventually
crash elsewhere in your code.
“.. the 64-bit version of the mburst
object is larger than the 32-bit
Even attempts to “be smart” by asking the sys-
version. This is because the size of
tem for the size of each member in the structure
the pointers and padding within
(as demonstrated in the following code) will not
the structure has increased to
succeed. This failure occurs because the
keep the members of the structure
solution does not take into account the padding
properly aligned....”
that is added on the 64-bit platform.
As a general rule, you should not try to outsmart the compiler. Modern compilers are
smart and will get things right more often than you might think. Consequently, to get the
size of a structure, use the sizeof() operator on the structure itself, as shown in the follow-
ing code. The sizeof() operator takes into account all of the padding that has been inserted
into a structure—including the padding that has sometimes been added to the end of a
structure.
mburst* create_mburst()
{ “... As a general rule,
mburst* p = (mburst*) malloc(sizeof(mburst)); you should not try to out-
p->seq_no = -1;
p->size = 0; smart the compiler. Mod-
p->data = NULL; ern compilers are very
p->next = NULL;
return p; smart and will get things
} right more often than you
might think.”
Upcoming Newsletter
The next 64-bit Insider newsletter discusses more align-
ment topics, such as hard-coding structure sizes in C++,
miscalculating offsets, custom layouts, packing, and alignment exceptions.
Recommended Reading
Larry Osterman’s Weblog: Alignment (part 1)
http://blogs.msdn.com/larryosterman/archive/2005/04/07/406252.aspx