|
|||||||||||
|
Technical Support On-Line Manuals Compiler Reference Guide |
Structures, unions, enumerations, and bitfields
This topic describes the implementation of the structured data types union, enum, and struct. It also discusses structure padding and bitfield implementation. See Anonymous classes, structures and unions for more information. When a member of a union is accessed using a member of a different type, the resulting value can be predicted from the representation of the original type. No error is given. An object of type enum is implemented in the smallest integral type that contains the range of the enum. In C mode, and in C++ mode without
Note
Implementing enum in this way can reduce data
size. The command-line option See the description of C language mappings in the Procedure Call Standard for the ARM Architecture specification for more information. NoteCare must be taken when mixing translation units that have
been compiled with and without the In strict C, enumerator values must be representable as ints,
for example, they must be in the range -2147483648 to +2147483647,
inclusive. In some earlier releases of RVCT out-of-range values
were cast to int without a warning (unless you specified
the In RVCT v2.2 and later, a Warning is issued for out-of-range enumerator values: #66: enumeration value is out of "int" range Such values are treated the same way as in C++, that is, they are treated as unsigned int, long long, or unsigned long long. To ensure that out-of-range Warnings are reported, use the following command to change them into Errors: armcc --diag_error=66 ... The following points apply to:
Structures can contain padding to ensure that fields are correctly
aligned and that the structure itself is correctly aligned. Figure 3 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 The compiler pads structures in one of the following ways, according to how the structure is defined:
Use the Structures with empty initializers are permitted in C++:
struct
{
int x;
} X = { };
However, if you are compiling C, or compiling C++ with the A packed structure is one where the alignment of the structure, and of the fields within it, is always 1. You can pack specific structures with the In nonpacked structures, the ARM compiler allocates bitfields in containers. A container is a correctly aligned object of a declared type. Bitfields are allocated so that the first field specified occupies the lowest-addressed bits of the word, depending on configuration:
A bitfield container can be any of the integral types. NoteIn strict 1990 ISO Standard C, the only types permitted for a bit field are int, signed int, and unsigned int. For non-int bitfields, the compiler displays an error. A plain bitfield, declared without either signed or unsigned qualifiers,
is treated as unsigned. For example, A bitfield is allocated to the first container of the correct type that has a sufficient number of unallocated bits, for example:
struct X
{
int x:10;
int y:20;
};
The first declaration creates an integer container and allocates
10 bits to A bitfield is wholly contained within its container. A bitfield
that does not fit in a container is placed in the next container
of the same type. For example, the declaration of
struct X
{
int x:10;
int y:20;
int z:5;
};
The compiler pads the remaining two bits for the first container
and assigns a new integer container for Bitfield containers can overlap each other, for example:
struct X
{
int x:10;
char y:2;
};
The first declaration creates an integer container and allocates
10 bits to Because the natural alignment of char is 1, the
compiler searches for the first byte that contains a sufficient
number of unallocated bits to completely contain the bitfield. In
the example structure, the second byte of the int container
has two bits allocated to If
struct X
{
int x:10;
char y:8;
};
NoteThe same basic rules apply to bitfield declarations with different container types. For example, adding an int bitfield to the example structure gives:
struct X
{
int x:10;
char y:8;
int z:5;
}
The compiler allocates an int container starting
at the same location as the You can explicitly pad a bitfield container by declaring an unnamed bitfield of size zero. A bitfield of zero size fills the container up to the end if the container is not empty. A subsequent bitfield declaration starts a new empty container. | ||||||||||
|
|||||||||||