Bit domain problems in C

Source: Internet
Author: User
Tags domain list

When storing some information, it does not need to occupy a full byte, but only needs to occupy a few or one binary bit. For example, when storing a switch value, there are only two States: 0 and 1. Use one binary digit. To save storage space and simplify processing, the C language also provides a data structure called "bit domain" or "bit segment ". The so-called "bit field" refers to dividing the binary character in a byte into several different regions and showing the digits of each region. Each domain has a domain name, which allows operations by domain name in the program. In this way, several different objects can be represented by a byte binary field.
The format is: struct bit domain structure name {bit domain list };
I. Reasons for memory alignment
Most of the references are as follows:
1. Platform reason (reason for transplantation): Not all hardware platforms can access any data on any address. Some hardware platforms can only retrieve certain types of data at some addresses, otherwise, a hardware exception is thrown.
2. Performance reasons: Data Structures (especially stacks) should be aligned on natural boundaries as much as possible. The reason is that in order to access non-alignment memory, the processor needs to perform two memory accesses, while alignment memory access only needs one access.
Ii. Alignment rules
Each compiler on a specific platform has its own default "alignment coefficient" (also called alignment modulus ). Programmers can use the pre-compiled command # pragma pack (n), n =, and 16 to change this coefficient. n is the alignment coefficient you want to specify ".
Rules:
1. Data member alignment rules: the data member of the structure (struct) (or union). The first data member is placed at the position where the offset is 0, in the future, the alignment of each data member is performed according to the value specified by # pragma pack and the smaller value of the data member's length.
2. Overall alignment rules of the structure (or union): After the data members align themselves, the structure (or union) itself also needs to be aligned, alignment is performed according to the value specified by # pragma pack and the smaller value in the maximum data member length of the structure (or union.
3. When the n value of # pragma pack is equal to or greater than the length of all data members, the n value does not have any effect.
Iii. Test
Let's illustrate this rule through a series of examples!
The compilers used in my experiment include C compilers of GCC 3.4.2 and VC6.0. The platform is Windows XP + Sp2.
We will use a typical struct pair to describe. First, we define a struct:
# Pragma pack (n)/* n = 1, 2, 4, 8, 16 */
Struct test_t {
Int;
Char B;
Short c;
Char d;
};
# Pragma pack (n)
First, we confirm the size of each type on the test platform. After verification, the output of both compilers is:
Sizeof (char) = 1
Sizeof (short) = 2
Sizeof (int) = 4
Our test process is as follows: Change the alignment coefficient by # pragma pack (n), and then check the value of sizeof (struct test_t.
1. 1-byte alignment (# pragma pack (1 ))
Output result: sizeof (struct test_t) = 8 [the two compilers output the same]
Analysis process:
1) Member Data Alignment
# Pragma pack (1)
Struct test_t {
Int a;/* length 4> 1 aligned by 1; Start offset = 0 0% 1 = 0; storage position range [] */
Char B;/* length 1 = 1 aligned by 1; Start offset = 4 4% 1 = 0; storage location range [4] */
Short c;/* length 2> 1 aligned by 1; Start offset = 5 5% 1 = 0; storage location range [5, 6] */
Char d;/* length 1 = 1 aligned by 1; Start offset = 7 7% 1 = 0; storage location range [7] */
};
# Pragma pack ()
Total member size = 8
2) overall alignment
Overall alignment coefficient = min (max (int, short, char), 1) = 1
Overall size (size) = $ (total member size) by $ (overall alignment coefficient) rounded = 8/* 8% 1 = 0 */[NOTE 1]
2. 2-byte alignment (# pragma pack (2 ))
Output result: sizeof (struct test_t) = 10 [the two compilers output the same]
Analysis process:
1) Member Data Alignment
# Pragma pack (2)
Struct test_t {
Int a;/* length 4> 2 aligned by 2; Start offset = 0 0% 2 = 0; storage position range [] */
Char B;/* length 1 <2 aligned by 1; Start offset = 4 4% 1 = 0; storage location range [4] */
Short c;/* length 2 = 2 aligned by 2; Start offset = 6 6% 2 = 0; storage location range [6, 7] */
Char d;/* length 1 <2 aligned by 1; Start offset = 8 8% 1 = 0; storage location range [8] */
};
# Pragma pack ()
Total member size = 9
2) overall alignment
Overall alignment coefficient = min (max (int, short, char), 2) = 2
Overall size (size) = $ (total member size) by $ (overall alignment coefficient) rounded = 10/* 10% 2 = 0 */
3. 4-byte alignment (# pragma pack (4 ))
Output result: sizeof (struct test_t) = 12 [the two compilers output the same]
Analysis process:
1) Member Data Alignment
# Pragma pack (4)
Struct test_t {
Int a;/* length 4 = 4 aligned by 4; Start offset = 0 0% 4 = 0; storage position range [] */
Char B;/* length 1 <4 aligned by 1; Start offset = 4 4% 1 = 0; storage location range [4] */
Short c;/* length 2 <4 aligned by 2; Start offset = 6 6% 2 = 0; storage location range [6, 7] */
Char d;/* length 1 <4 aligned by 1; Start offset = 8 8% 1 = 0; storage location range [8] */
};
# Pragma pack ()
Total member size = 9
2) overall alignment
Overall alignment coefficient = min (max (int, short, char), 4) = 4
Overall size (size) = $ (total member size) by $ (overall alignment coefficient) rounded = 12/* 12% 4 = 0 */
4. 8-byte alignment (# pragma pack (8 ))
Output result: sizeof (struct test_t) = 12 [the two compilers output the same]
Analysis process:
1) Member Data Alignment
# Pragma pack (8)
Struct test_t {
Int a;/* length 4 <8 aligned by 4; Start offset = 0 0% 4 = 0; storage position range [] */
Char B;/* length 1 <8 aligned by 1; Start offset = 4 4% 1 = 0; storage location range [4] */
Short c;/* length 2 <8 aligned by 2; Start offset = 6 6% 2 = 0; storage location range [6, 7] */
Char d;/* length 1 <8 aligned by 1; Start offset = 8 8% 1 = 0; storage location range [8] */
};
# Pragma pack ()
Total member size = 9
2) overall alignment
Overall alignment coefficient = min (max (int, short, char), 8) = 4
Overall size (size) = $ (total member size) by $ (overall alignment coefficient) rounded = 12/* 12% 4 = 0 */
5. 16-byte alignment (# pragma pack (16 ))
Output result: sizeof (struct test_t) = 12 [the two compilers output the same]
Analysis process:
1) Member Data Alignment
# Pragma pack (16)
Struct test_t {
Int a;/* length 4 <16 aligned by 4; Start offset = 0 0% 4 = 0; storage position range [] */
Char B;/* 1 <16 aligned by 1; Start offset = 4 4% 1 = 0; storage location range [4] */
Short c;/* 2 <16 aligned by 2; Start offset = 6 6% 2 = 0; storage location range [6, 7] */
Char d;/* 1 <16 aligned by 1; Start offset = 8 8% 1 = 0; storage location range [8] */
};
# Pragma pack ()
Total member size = 9
2) overall alignment
Overall alignment coefficient = min (max (int, short, char), 16) = 4
Overall size (size) = $ (total member size) by $ (overall alignment coefficient) rounded = 12/* 12% 4 = 0 */
Iv. Conclusion
The 8-byte and 16-byte alignment test proves the 3rd point of the "rule": "When the # pragma pack n value equals or exceeds the length of all data members, the size of this n value will not produce any effect ".
[NOTE 1]
What is "circle "?
Example: in the above 8-byte alignment, "overall alignment" indicates that the overall size is 9 and rounded to 12.
The process of rounding: Start from 9 and add one at a time to see if it can be divisible by 4. Here, 9, 10, and 11 cannot be divisible by 4. By 12, the circle ends.

Common alignment modes (vc6.0, 32-bit system ).

Alignment (offset of the starting address of the variable to the starting address of the structure)
The Char offset must be sizeof (char), that is, a multiple of 1.
The int offset must be a multiple of sizeof (int), that is, 4.
The float offset must be a multiple of sizeof (float), that is, 4.
The double offset must be sizeof (double), that is, a multiple of 8.
The Short offset must be a multiple of sizeof (short), that is, 2.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.