Language Implementation
7-21
7
To get the alignment
a
for a struct or union
u
, given #pragma align
n
:
• let
m
be the largest alignment of all members of
u
.
• let
s
be
u
's unpadded size rounded up to the next power of 2.
• then
align(u) = max (m, min (n, s)).
Thus, a structure can never be given an alignment requirement that is less
than the largest alignment required for any of its members;
#pragma
align
can be used only to limit the amount of extra padding added to
improve the alignment of the entire structure. Note that restricting
structure alignment padding can affect the size and performance of the
generated code.
The following examples show how
#pragma align can affect the
allocation of structs.
struct s0{ struct s1{ struct s2{
char x[9]; char x[8]; char y;
}; struct s0 z; short z;
}; short zz;
};
#pragma: size: size:
align 1
align 2
align 4
align 8
align 16
9
10
12
16
16
17
18
20
24
32
#pragma align does not restrict the alignment of individual static,
extern, or auto variable allocations that happen to be structures. The
compiler aligns each separate memory variable allocation based strictly on
the size of the allocation, without regard to the formal alignment
requirement of the variable's type.