Datasheet
ARM Compiler Reference
ARM DUI 0067D Copyright © 1999-2001 ARM Limited. All rights reserved. 3-27
• all C++ structures and classes not using virtual functions or base classes.
Structure alignment
The alignment of a nonpacked structure is the maximum alignment
required by any of its fields.
Field alignment
Structures are arranged with the first-named component at the lowest
address. Fields are aligned as follows:
• A field with a
char
type is aligned to the next available byte.
• A field with a
short
type is aligned to the next even-addressed byte.
• Bitfield alignment depends on how the bitfield is declared. See
Bitfields in packed structures on page 3-30 for more information.
• All other types are aligned on word boundaries.
Structures can contain padding to ensure that fields are correctly aligned and that the
structure itself is correctly aligned. Figure 3-1 shows an example of a conventional,
nonpacked structure. Bytes 1, 2, and 3 are padded to ensure correct field alignment.
Bytes 11 and 12 are padded to ensure correct structure alignment. The
sizeof()
function
returns the size of the structure including padding.
The compiler pads structures in one of two ways, according to how the structure is
defined:
• Structures that are defined as
static
or
extern
are padded with zeros.
• Structures on the stack or heap, such as those defined with
malloc()
or
auto
, are
padded with whatever was previously stored in those memory locations. You
cannot use
memcmp()
to compare padded structures defined in this way
(Figure 3-1). Use the
-W+s
option to generate a warning when the compiler inserts
padding in a
struct
.
Figure 3-1 Conventional structure example
• Structures with empty initializers are allowed in C++ and only warned about in C
(if C and
-strict
an error is generated):
struct { int x; } X = { };
0 1 2 3
c
s
x
padding
struct {char c; int x; short s} ex1;
padding
4 5 7 8
9 10 11 12