C-language Median field definition: The form of a declaration variable defined in a struct: type specifier bit domain name: bit field width
struct bit{
int a:4; Declare bit-domain variable A, which takes up to 4 bit width
int b:4; Declare bit-domain variable B, which occupies 4 bit-bit width
int c:4; Declare bit field variable C, occupy 4 bit width
};
The default alignment of the struct is performed in the GCC compiler, and all of the following are based on the GCC compiler bar
Thing
1): The two connected bit-domain variable placeholders are generally tightly connected, such as a and b above, or B and
C:a occupies the lower 4 bits of the starting byte, B occupies a high 4 bits of the starting byte, and C occupies the lower 4 bits of the second byte,
Closely connected in turn.
2): If a bit domain variable occupies no more than 1 bytes, then the placeholder allocation of this variable will not span two
Byte, which means that he must be in a byte, such as the above B if the placeholder is 5, if you press 1 directly
) said that his placeholder allocation would span different bytes, in which case the allocation should start from the next byte;
If the placeholder for a bit field variable exceeds one byte, the placeholder allocation cannot be followed directly by the previous variable.
Instead, the placeholder should start with the next byte, so this passage means: If a bit field variable occupies more than
The bit bit after the last bit field variable placeholder (I'm talking about the remainder in a byte)
The volume should be occupied from the next "clean" byte. So what I said in 1 is general.
3): How to determine the size of the whole structure:
In fact, the overall structure size, not the number of bits in the interior of this simple, the compiler will be
A byte-aligned operation, followed by a normal struct-byte alignment, is quite different:
If we declare several bit-domain variable placeholders in a row, the sum of the largest of the types in these variables is not exceeded
The total size of the variables of these successive declarations is the size of this type, for example:
struct bit{
int a:4;
int b:4;
Char C:4;
Char D:4;
}
According to the method above, the longest type of these variables is int, and their bit lengths are added together and do not exceed
The length of the int type, so the size of this struct is 4 bytes. If this is exceeded, for example, the following scenario:
struct bit{
int a:8;
int b:8;
int c:8;
Char D:8;
Char E:3;
};
So this is the case: the first 4 variables occupy just the size of int, divided into a group, and the back of the E and
is divided into groups, and is determined by the method above to determine the size of the group, the largest type of this group is char, and
E does not exceed the size of char, so it is 1 bytes, so the size of the entire struct is 4+1 = 5 bytes.
3): We can declare a variable without a bit domain name in the struct, for example:
struct bit{
int a:4;
int b:4;
int:0;
Int:2;
};
So at this point the non-name of the bit field variable is not to be used, he is only used to occupy a bit, if it is accounted for
The unknown bit field variable bit 0, for example, 3rd, int:0, though also a placeholder, is slightly more complex:
This variable means that the new variable is allocated from a new byte, but this specific new byte is the word
section, which is not necessarily the next byte, is related to the specific type modifier, if it is char-modified, then it is
Starting with the next byte, look at the example, I don't know how to express it.
struct bit{
int a:8;
int b:8;
int:0; Start a placeholder after 4 bytes from the beginning of a
};
struct bit{
int a:8;
int b:8;
char:0; Starting at 1 bytes from the start of B and beginning the placeholder
};
struct bit{
int a:8;
int b:8;
int c:8;
short:0; Starting at 2 bytes from the start of B and beginning the placeholder
};
struct bit{
int a:8;
int b:8;
int c:8;
int d:8;
int:0; Start a placeholder after 4 bytes from the beginning of a
};
struct bit{
int a:8;
int b:8;
int c:8;
int d:8;
int e:8;
int:0; The placeholder starts after 4 bytes from the start of E, since the first 4 are already in a group
The
};
struct bit{
int a:8;
int b:8;
int c:8;
int d:8;
int e:8;
short:0; The placeholder starts after 2 bytes from the start of E, since the first 4 are already one
Group of
};
struct bit{
int a:8;
int b:8;
int c:8;
int d:8;
int e:8;
char:0; The placeholder starts after 1 bytes from the start of E, since the first 4 are already one
Group of
};
It is important to note that empty space is also a space for this structure.
C-Language middle-Field Operation