Download RealView Compilation Tools Compiler Reference Guide
Transcript
C and C++ Implementation Details
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 z.
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 x. These 10 bits
occupy the first byte and two bits of the second byte of the integer container. At the
second declaration, the compiler checks for a container of type char. There is no suitable
container, so the compiler allocates a new correctly aligned char container.
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 x, and
six bits unallocated. The compiler allocates a char container starting at the second byte
of the previous int container, skips the first two bits that are allocated to x, and allocates
two bits to y.
If y is declared char y:8, the compiler pads the second byte and allocates a new char
container to the third byte, because the bitfield cannot overflow its container. Figure 5-2
shows the bitfield allocation for the following example structure:
struct X
{
int x:10;
char y:8;
};
Bit number
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
unallocated
y
padding
x
Figure 5-2 Bitfield allocation 1
ARM DUI 0348C
ID101213
Copyright © 2007-2010 ARM. All rights reserved.
Non-Confidential,
5-11