<s>
Data	B-Application
structure	I-Application
alignment	I-Application
is	O
the	O
way	O
data	O
is	O
arranged	O
and	O
accessed	O
in	O
computer	B-General_Concept
memory	I-General_Concept
.	O
</s>
<s>
It	O
consists	O
of	O
three	O
separate	O
but	O
related	O
issues	O
:	O
data	B-Application
alignment	I-Application
,	O
data	B-General_Concept
structure	I-General_Concept
padding	O
,	O
and	O
packing	B-Application
.	O
</s>
<s>
The	O
CPU	B-General_Concept
in	O
modern	O
computer	O
hardware	O
performs	O
reads	O
and	O
writes	O
to	O
memory	O
most	O
efficiently	O
when	O
the	O
data	O
is	O
naturally	O
aligned	O
,	O
which	O
generally	O
means	O
that	O
the	O
data	O
's	O
memory	O
address	O
is	O
a	O
multiple	O
of	O
the	O
data	O
size	O
.	O
</s>
<s>
For	O
instance	O
,	O
in	O
a	O
32-bit	O
architecture	O
,	O
the	O
data	O
may	O
be	O
aligned	O
if	O
the	O
data	O
is	O
stored	O
in	O
four	O
consecutive	O
bytes	B-Application
and	O
the	O
first	O
byte	B-Application
lies	O
on	O
a	O
4-byte	O
boundary	O
.	O
</s>
<s>
Data	B-Application
alignment	I-Application
is	O
the	O
aligning	O
of	O
elements	O
according	O
to	O
their	O
natural	O
alignment	O
.	O
</s>
<s>
For	O
example	O
,	O
on	O
a	O
32-bit	O
machine	O
,	O
a	O
data	B-General_Concept
structure	I-General_Concept
containing	O
a	O
16-bit	O
value	O
followed	O
by	O
a	O
32-bit	O
value	O
could	O
have	O
16	O
bits	O
of	O
padding	O
between	O
the	O
16-bit	O
value	O
and	O
the	O
32-bit	O
value	O
to	O
align	O
the	O
32-bit	O
value	O
on	O
a	O
32-bit	O
boundary	O
.	O
</s>
<s>
Although	O
data	B-Application
structure	I-Application
alignment	I-Application
is	O
a	O
fundamental	O
issue	O
for	O
all	O
modern	O
computers	O
,	O
many	O
computer	O
languages	O
and	O
computer	O
language	O
implementations	O
handle	O
data	B-Application
alignment	I-Application
automatically	O
.	O
</s>
<s>
Fortran	B-Application
,	O
Ada	B-Language
,	O
PL/I	B-Language
,	O
Pascal	B-Application
,	O
certain	O
C	B-Language
and	O
C++	B-Language
implementations	O
,	O
D	B-Application
,	O
Rust	B-Application
,	O
C#	B-Application
,	O
and	O
assembly	B-Language
language	I-Language
allow	O
at	O
least	O
partial	O
control	O
of	O
data	B-General_Concept
structure	I-General_Concept
padding	O
,	O
which	O
may	O
be	O
useful	O
in	O
certain	O
special	O
circumstances	O
.	O
</s>
<s>
A	O
memory	O
address	O
a	O
is	O
said	O
to	O
be	O
n-byte	O
aligned	O
when	O
a	O
is	O
a	O
multiple	O
of	O
n	O
(	O
where	O
n	O
is	O
a	O
power	O
of	O
2	O
)	O
.	O
</s>
<s>
In	O
this	O
context	O
,	O
a	O
byte	B-Application
is	O
the	O
smallest	O
unit	O
of	O
memory	O
access	O
,	O
i.e.	O
</s>
<s>
each	O
memory	O
address	O
specifies	O
a	O
different	O
byte	B-Application
.	O
</s>
<s>
An	O
n-byte	O
aligned	O
address	O
would	O
have	O
a	O
minimum	O
of	O
least-significant	O
zeros	O
when	O
expressed	O
in	O
binary	O
.	O
</s>
<s>
The	O
alternate	O
wording	O
b-bit	O
aligned	O
designates	O
a	O
b/8	O
byte	B-Application
aligned	O
address	O
(	O
ex	O
.	O
</s>
<s>
64-bit	B-Device
aligned	O
is	O
8bytes	O
aligned	O
)	O
.	O
</s>
<s>
A	O
memory	O
access	O
is	O
said	O
to	O
be	O
aligned	O
when	O
the	O
data	O
being	O
accessed	O
is	O
nbytes	O
long	O
and	O
the	O
datum	O
address	O
is	O
n-byte	O
aligned	O
.	O
</s>
<s>
Note	O
that	O
by	O
definition	O
byte	B-Application
memory	O
accesses	O
are	O
always	O
aligned	O
.	O
</s>
<s>
A	O
memory	O
pointer	O
that	O
refers	O
to	O
primitive	O
data	O
that	O
is	O
nbytes	O
long	O
is	O
said	O
to	O
be	O
aligned	O
if	O
it	O
is	O
only	O
allowed	O
to	O
contain	O
addresses	O
that	O
are	O
n-byte	O
aligned	O
,	O
otherwise	O
it	O
is	O
said	O
to	O
be	O
unaligned	O
.	O
</s>
<s>
A	O
memory	O
pointer	O
that	O
refers	O
to	O
a	O
data	O
aggregate	O
(	O
a	O
data	B-General_Concept
structure	I-General_Concept
or	O
array	O
)	O
is	O
aligned	O
if	O
(	O
and	O
only	O
if	O
)	O
each	O
primitive	O
datum	O
in	O
the	O
aggregate	O
is	O
aligned	O
.	O
</s>
<s>
Note	O
that	O
the	O
definitions	O
above	O
assume	O
that	O
each	O
primitive	O
datum	O
is	O
a	O
power	O
of	O
two	O
bytes	B-Application
long	O
.	O
</s>
<s>
When	O
this	O
is	O
not	O
the	O
case	O
(	O
as	O
with	O
80-bit	O
floating-point	O
on	O
x86	B-Operating_System
)	O
the	O
context	O
influences	O
the	O
conditions	O
where	O
the	O
datum	O
is	O
considered	O
aligned	O
or	O
not	O
.	O
</s>
<s>
Data	B-General_Concept
structures	I-General_Concept
can	O
be	O
stored	O
in	O
memory	O
on	O
the	O
stack	O
with	O
a	O
static	O
size	O
known	O
as	O
bounded	O
or	O
on	O
the	O
heap	O
with	O
a	O
dynamic	O
size	O
known	O
as	O
unbounded	O
.	O
</s>
<s>
The	O
CPU	B-General_Concept
accesses	O
memory	O
by	O
a	O
single	O
memory	O
word	O
at	O
a	O
time	O
.	O
</s>
<s>
If	O
the	O
highest	O
and	O
lowest	O
bytes	B-Application
in	O
a	O
datum	O
are	O
not	O
within	O
the	O
same	O
memory	O
word	O
the	O
computer	O
must	O
split	O
the	O
datum	O
access	O
into	O
multiple	O
memory	O
accesses	O
.	O
</s>
<s>
To	O
handle	O
the	O
case	O
where	O
the	O
memory	O
words	O
are	O
in	O
different	O
memory	O
pages	O
the	O
processor	O
must	O
either	O
verify	O
that	O
both	O
pages	O
are	O
present	O
before	O
executing	O
the	O
instruction	O
or	O
be	O
able	O
to	O
handle	O
a	O
TLB	B-Architecture
miss	O
or	O
a	O
page	B-General_Concept
fault	I-General_Concept
on	O
any	O
memory	O
access	O
during	O
the	O
instruction	O
execution	O
.	O
</s>
<s>
For	O
example	O
,	O
implementations	O
of	O
the	O
ARM	O
architecture	O
prior	O
to	O
the	O
ARMv6	O
ISA	O
require	O
mandatory	O
aligned	O
memory	O
access	O
for	O
all	O
multi-byte	O
load	O
and	O
store	O
instructions	O
.	O
</s>
<s>
Depending	O
on	O
which	O
specific	O
instruction	O
was	O
issued	O
,	O
the	O
result	O
of	O
attempted	O
misaligned	O
access	O
might	O
be	O
to	O
round	O
down	O
the	O
least	O
significant	O
bits	O
of	O
the	O
offending	O
address	O
turning	O
it	O
into	O
an	O
aligned	O
access	O
(	O
sometimes	O
with	O
additional	O
caveats	O
)	O
,	O
or	O
to	O
throw	O
an	O
MMU	B-General_Concept
exception	O
(	O
if	O
MMU	B-General_Concept
hardware	O
is	O
present	O
)	O
,	O
or	O
to	O
silently	O
yield	O
other	O
potentially	O
unpredictable	O
results	O
.	O
</s>
<s>
Although	O
the	O
compiler	B-Language
(	O
or	O
interpreter	B-Application
)	O
normally	O
allocates	O
individual	O
data	O
items	O
on	O
aligned	O
boundaries	O
,	O
data	B-General_Concept
structures	I-General_Concept
often	O
have	O
members	O
with	O
different	O
alignment	O
requirements	O
.	O
</s>
<s>
In	O
addition	O
,	O
the	O
data	B-General_Concept
structure	I-General_Concept
as	O
a	O
whole	O
may	O
be	O
padded	O
with	O
a	O
final	O
unnamed	O
member	O
.	O
</s>
<s>
This	O
allows	O
each	O
member	O
of	O
an	O
array	B-General_Concept
of	I-General_Concept
structures	I-General_Concept
to	O
be	O
properly	O
aligned	O
.	O
</s>
<s>
Although	O
C	B-Language
and	O
C++	B-Language
do	O
not	O
allow	O
the	O
compiler	B-Language
to	O
reorder	O
structure	O
members	O
to	O
save	O
space	O
,	O
other	O
languages	O
might	O
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
to	O
tell	O
most	O
C	B-Language
and	O
C++	B-Language
compilers	B-Language
to	O
"	O
pack	O
"	O
the	O
members	O
of	O
a	O
structure	O
to	O
a	O
certain	O
level	O
of	O
alignment	O
,	O
e.g.	O
</s>
<s>
"	O
pack(2 )	O
"	O
means	O
align	O
data	O
members	O
larger	O
than	O
a	O
byte	B-Application
to	O
a	O
two-byte	O
boundary	O
so	O
that	O
any	O
padding	O
members	O
are	O
at	O
most	O
one	O
byte	B-Application
long	O
.	O
</s>
<s>
Likewise	O
,	O
in	O
PL/I	B-Language
a	O
structure	O
may	O
be	O
declared	O
UNALIGNED	O
to	O
eliminate	O
all	O
padding	O
except	O
around	O
bit	O
strings	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
structure	O
containing	O
a	O
single	O
byte	B-Application
(	O
such	O
as	O
a	O
char	O
)	O
and	O
a	O
four-byte	O
integer	O
(	O
such	O
as	O
uint32_t	O
)	O
would	O
require	O
three	O
additional	O
bytes	B-Application
of	O
padding	O
.	O
</s>
<s>
Although	O
use	O
of	O
"	O
packed	O
"	O
structures	O
is	O
most	O
frequently	O
used	O
to	O
conserve	O
memory	O
space	O
,	O
it	O
may	O
also	O
be	O
used	O
to	O
format	O
a	O
data	B-General_Concept
structure	I-General_Concept
for	O
transmission	O
using	O
a	O
standard	O
protocol	O
.	O
</s>
<s>
However	O
,	O
in	O
this	O
usage	O
,	O
care	O
must	O
also	O
be	O
taken	O
to	O
ensure	O
that	O
the	O
values	O
of	O
the	O
struct	B-Application
members	O
are	O
stored	O
with	O
the	O
endianness	O
required	O
by	O
the	O
protocol	O
(	O
often	O
network	O
byte	B-Application
order	O
)	O
,	O
which	O
may	O
be	O
different	O
from	O
the	O
endianness	O
used	O
natively	O
by	O
the	O
host	O
machine	O
.	O
</s>
<s>
The	O
following	O
formulas	O
provide	O
the	O
number	O
of	O
padding	O
bytes	B-Application
required	O
to	O
align	O
the	O
start	O
of	O
a	O
data	B-General_Concept
structure	I-General_Concept
(	O
where	O
mod	O
is	O
the	O
modulo	O
operator	O
)	O
:	O
</s>
<s>
For	O
example	O
,	O
the	O
padding	O
to	O
add	O
to	O
offset	O
0x59d	O
for	O
a	O
4-byte	O
aligned	O
structure	O
is	O
3	O
.	O
</s>
<s>
The	O
following	O
formulas	O
produce	O
the	O
correct	O
values	O
(	O
where	O
&	O
is	O
a	O
bitwise	O
AND	O
and	O
~	O
a	O
bitwise	O
NOT	O
)	O
--	O
providing	O
the	O
offset	O
is	O
unsigned	O
or	O
the	O
system	O
uses	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
arithmetic	I-General_Concept
:	O
</s>
<s>
Data	B-General_Concept
structure	I-General_Concept
members	O
are	O
stored	O
sequentially	O
in	O
memory	O
so	O
that	O
,	O
in	O
the	O
structure	O
below	O
,	O
the	O
member	O
Data1	O
will	O
always	O
precede	O
Data2	O
;	O
and	O
Data2	O
will	O
always	O
precede	O
Data3	O
:	O
</s>
<s>
If	O
the	O
type	O
"	O
short	O
"	O
is	O
stored	O
in	O
two	O
bytes	B-Application
of	O
memory	O
then	O
each	O
member	O
of	O
the	O
data	B-General_Concept
structure	I-General_Concept
depicted	O
above	O
would	O
be	O
2-byte	O
aligned	O
.	O
</s>
<s>
The	O
following	O
typical	O
alignments	O
are	O
valid	O
for	O
compilers	B-Language
from	O
Microsoft	O
(	O
Visual	B-Application
C++	I-Application
)	O
,	O
Borland/CodeGear	O
(	O
C++Builder	B-Language
)	O
,	O
Digital	B-Language
Mars	I-Language
(	O
DMC	O
)	O
,	O
and	O
GNU	B-Operating_System
(	O
GCC	B-Application
)	O
when	O
compiling	B-Language
for	O
32-bit	O
x86	B-Operating_System
:	O
</s>
<s>
A	O
char	O
(	O
one	O
byte	B-Application
)	O
will	O
be	O
1-byte	O
aligned	O
.	O
</s>
<s>
A	O
short	O
(	O
two	O
bytes	B-Application
)	O
will	O
be	O
2-byte	O
aligned	O
.	O
</s>
<s>
An	O
int	O
(	O
four	O
bytes	B-Application
)	O
will	O
be	O
4-byte	O
aligned	O
.	O
</s>
<s>
A	O
long	O
(	O
four	O
bytes	B-Application
)	O
will	O
be	O
4-byte	O
aligned	O
.	O
</s>
<s>
A	O
float	O
(	O
four	O
bytes	B-Application
)	O
will	O
be	O
4-byte	O
aligned	O
.	O
</s>
<s>
A	O
double	O
(	O
eight	O
bytes	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
on	O
Windows	O
and	O
4-byte	O
aligned	O
on	O
Linux	O
(	O
8-byte	O
with	O
-malign-double	O
compile	B-Language
time	O
option	O
)	O
.	O
</s>
<s>
A	O
long	O
long	O
(	O
eight	O
bytes	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
on	O
Windows	O
and	O
4-byte	O
aligned	O
on	O
Linux	O
(	O
8-byte	O
with	O
-malign-double	O
compile	B-Language
time	O
option	O
)	O
.	O
</s>
<s>
A	O
long	O
double	O
(	O
ten	O
bytes	B-Application
with	O
C++Builder	B-Language
and	O
DMC	O
,	O
eight	O
bytes	B-Application
with	O
Visual	B-Application
C++	I-Application
,	O
twelve	O
bytes	B-Application
with	O
GCC	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
with	O
C++Builder	B-Language
,	O
2-byte	O
aligned	O
with	O
DMC	O
,	O
8-byte	O
aligned	O
with	O
Visual	B-Application
C++	I-Application
,	O
and	O
4-byte	O
aligned	O
with	O
GCC	B-Application
.	O
</s>
<s>
Any	O
pointer	O
(	O
four	O
bytes	B-Application
)	O
will	O
be	O
4-byte	O
aligned	O
.	O
</s>
<s>
The	O
only	O
notable	O
differences	O
in	O
alignment	O
for	O
an	O
LP64	O
64-bit	B-Device
system	O
when	O
compared	O
to	O
a	O
32-bit	O
system	O
are	O
:	O
</s>
<s>
A	O
long	O
(	O
eight	O
bytes	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
.	O
</s>
<s>
A	O
double	O
(	O
eight	O
bytes	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
.	O
</s>
<s>
A	O
long	O
long	O
(	O
eight	O
bytes	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
.	O
</s>
<s>
A	O
long	O
double	O
(	O
eight	O
bytes	B-Application
with	O
Visual	B-Application
C++	I-Application
,	O
sixteen	O
bytes	B-Application
with	O
GCC	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
with	O
Visual	B-Application
C++	I-Application
and	O
16-byte	O
aligned	O
with	O
GCC	B-Application
.	O
</s>
<s>
Any	O
pointer	O
(	O
eight	O
bytes	B-Application
)	O
will	O
be	O
8-byte	O
aligned	O
.	O
</s>
<s>
Here	O
is	O
a	O
structure	O
with	O
members	O
of	O
various	O
types	O
,	O
totaling	O
8	O
bytes	B-Application
before	O
compilation	B-Language
:	O
</s>
<s>
After	O
compilation	B-Language
the	O
data	B-General_Concept
structure	I-General_Concept
will	O
be	O
supplemented	O
with	O
padding	O
bytes	B-Application
to	O
ensure	O
a	O
proper	O
alignment	O
for	O
each	O
of	O
its	O
members	O
:	O
</s>
<s>
The	O
compiled	B-Language
size	O
of	O
the	O
structure	O
is	O
now	O
12bytes	O
.	O
</s>
<s>
It	O
is	O
important	O
to	O
note	O
that	O
the	O
last	O
member	O
is	O
padded	O
with	O
the	O
number	O
of	O
bytes	B-Application
required	O
so	O
that	O
the	O
total	O
size	O
of	O
the	O
structure	O
should	O
be	O
a	O
multiple	O
of	O
the	O
largest	O
alignment	O
of	O
any	O
structure	O
member	O
(alignment(int )	O
in	O
this	O
case	O
,	O
which	O
=	O
4	O
on	O
linux-32bit/gcc	O
)	O
.	O
</s>
<s>
In	O
this	O
example	O
the	O
total	O
size	O
of	O
the	O
structure	O
,	O
not	O
5	O
(	O
not	O
8	O
either	O
)	O
(	O
so	O
that	O
the	O
size	O
is	O
a	O
multiple	O
of	O
2	O
(alignment(short )	O
=	O
2	O
on	O
linux-32bit/gcc	O
)	O
)	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
change	O
the	O
alignment	O
of	O
structures	O
to	O
reduce	O
the	O
memory	O
they	O
require	O
(	O
or	O
to	O
conform	O
to	O
an	O
existing	O
format	O
)	O
by	O
reordering	O
structure	O
members	O
or	O
changing	O
the	O
compiler	B-Language
's	O
alignment	O
(	O
or	O
“	O
packing	B-Application
”	O
)	O
of	O
structure	O
members	O
.	O
</s>
<s>
The	O
compiled	B-Language
size	O
of	O
the	O
structure	O
now	O
matches	O
the	O
pre-compiled	O
size	O
of	O
8	O
bytes	B-Application
.	O
</s>
<s>
The	O
alternative	O
method	O
of	O
enforcing	O
the	O
structure	O
to	O
be	O
aligned	O
to	O
a	O
one	O
byte	B-Application
boundary	O
will	O
cause	O
the	O
pre-processor	O
to	O
discard	O
the	O
pre-determined	O
alignment	O
of	O
the	O
structure	O
members	O
and	O
thus	O
no	O
padding	O
bytes	B-Application
would	O
be	O
inserted	O
.	O
</s>
<s>
While	O
there	O
is	O
no	O
standard	O
way	O
of	O
defining	O
the	O
alignment	O
of	O
structure	O
members	O
,	O
some	O
compilers	B-Language
use	O
directives	O
to	O
specify	O
packing	B-Application
inside	O
source	O
files	O
.	O
</s>
<s>
This	O
structure	O
would	O
have	O
a	O
compiled	B-Language
size	O
of	O
6	O
bytes	B-Application
on	O
a	O
32-bit	O
system	O
.	O
</s>
<s>
The	O
above	O
directives	O
are	O
available	O
in	O
compilers	B-Language
from	O
Microsoft	O
,	O
Borland	O
,	O
GNU	B-Operating_System
,	O
and	O
many	O
others	O
.	O
</s>
<s>
On	O
some	O
Microsoft	O
compilers	B-Language
,	O
particularly	O
for	O
RISC	O
processors	O
,	O
there	O
is	O
an	O
unexpected	O
relationship	O
between	O
project	O
default	O
packing	B-Application
(	O
the	O
/Zp	O
directive	O
)	O
and	O
the	O
directive	O
.	O
</s>
<s>
The	O
directive	O
can	O
only	O
be	O
used	O
to	O
reduce	O
the	O
packing	B-Application
size	O
of	O
a	O
structure	O
from	O
the	O
project	O
default	O
packing	B-Application
.	O
</s>
<s>
This	O
leads	O
to	O
interoperability	O
problems	O
with	O
library	O
headers	O
which	O
use	O
,	O
for	O
example	O
,	O
,	O
if	O
the	O
project	O
packing	B-Application
is	O
smaller	O
than	O
this	O
.	O
</s>
<s>
For	O
this	O
reason	O
,	O
setting	O
the	O
project	O
packing	B-Application
to	O
any	O
value	O
other	O
than	O
the	O
default	O
of	O
8bytes	O
would	O
break	O
the	O
directives	O
used	O
in	O
library	O
headers	O
and	O
result	O
in	O
binary	O
incompatibilities	O
between	O
structures	O
.	O
</s>
<s>
This	O
limitation	O
is	O
not	O
present	O
when	O
compiling	B-Language
for	O
x86	B-Operating_System
.	O
</s>
<s>
It	O
would	O
be	O
beneficial	O
to	O
allocate	O
memory	O
aligned	O
to	O
cache	B-General_Concept
lines	I-General_Concept
.	O
</s>
<s>
If	O
an	O
array	O
is	O
partitioned	O
for	O
more	O
than	O
one	O
thread	O
to	O
operate	O
on	O
,	O
having	O
the	O
sub-array	O
boundaries	O
unaligned	O
to	O
cache	B-General_Concept
lines	I-General_Concept
could	O
lead	O
to	O
performance	O
degradation	O
.	O
</s>
<s>
Alignment	O
concerns	O
can	O
affect	O
areas	O
much	O
larger	O
than	O
a	O
C	B-Language
structure	O
when	O
the	O
purpose	O
is	O
the	O
efficient	O
mapping	O
of	O
that	O
area	O
through	O
a	O
hardware	O
address	O
translation	O
mechanism	O
(	O
PCI	O
remapping	O
,	O
operation	O
of	O
a	O
MMU	B-General_Concept
)	O
.	O
</s>
<s>
For	O
instance	O
,	O
on	O
a	O
32-bit	O
operating	O
system	O
,	O
a	O
4KiB	O
(	O
4096	O
Bytes	B-Application
)	O
page	O
is	O
not	O
just	O
an	O
arbitrary	O
4KiB	O
chunk	O
of	O
data	O
.	O
</s>
<s>
Example	O
:	O
Assume	O
that	O
we	O
have	O
a	O
TLB	B-Architecture
mapping	O
of	O
virtual	O
address	O
0x2CFC7000	O
to	O
physical	O
address	O
0x12345000	O
.	O
</s>
<s>
Accessing	O
data	O
located	O
at	O
virtual	O
address	O
va	O
=	O
0x2CFC7ABC	O
causes	O
a	O
TLB	B-Architecture
resolution	O
of	O
0x2CFC7	O
to	O
0x12345	O
to	O
issue	O
a	O
physical	O
access	O
to	O
pa	O
=	O
0x12345ABC	O
.	O
</s>
