The Bit field field of the calculation of the structure body size

Source: Internet
Author: User

Classes and structs can contain members that occupy less storage space than an integral type. These members are specified as bit fields. The syntax for the bit-field member declaration specification is as follows: syntax

declarator  : Constant-expression

The following example declares the structure that contains a bit field:

Bit_fields1.cpp
//compile with:/ld
struct Date {
   unsigned short nweekday  : 3;    0..7   (3 bits)
   unsigned short nmonthday:6;    0..31  (6 bits)
   unsigned short nmonth    : 5;    0..12  (5 bits)
   unsigned short nyear     : 8;    0..100 (8 bits)
};

The conceptual memory layout of a date-type object is shown in the following illustration.

Content layout of Data Objects

Note that the length of the nyear is 8 bits and overflows the word boundary of the declaring type unsigned short. Therefore, it begins with the beginning of the new unsigned short. It does not have to fit all the bit fields to an object of the underlying type, and allocates a new storage unit based on the number of bits requested in the Declaration, so the structure body size is 4 bytes. The data declared as a bit field is sorted from low to high, as shown in the figure above.


If the declaration of a struct contains an unnamed field with a length of 0 (as shown in the following example),

Bit_fields2.cpp
//compile with:/ld
struct Date {
   unsigned nweekday  : 3;    0..7   (3 bits)
   unsigned nmonthday:6;    0..31  (6 bits)
   unsigned           : 0;    Force alignment to next boundary.
   Unsigned nmonth    : 5;    0..12  (5 bits)
   unsigned nyear     : 8;    0..100 (8 bits)
};

The memory layout is shown in the following figure.

Layout of data Objects with 0-bit-length fields

The underlying type of a bit field must be an integral type, as described in the base type.

Summarized as follows

The primary purpose of using a bit field is to compress storage, with the following general rules:
1 if the adjacent bit field fields are of the same type, and its bit width is less than the sizeof size of the type, the following field is stored next to the previous field until it cannot be accommodated; 2) if the adjacent bit field field is of the same type, but its bit width is greater than the type's sizeof size, Then the field will start with the new storage unit. An integer number whose offset is the size of its type; 3 if the adjacent bit field fields are of different types, the specific implementations of each compiler differ, the VC6 takes the uncompressed approach, the dev-c++ adopts the compression method, and 4 If the bit field fields are interspersed with a non-bit field field, No compression, 5) The total size of the entire structure is an integer multiple of the widest base type member size. The case is as follows:[CPP]  View Plain  copy   typedef struct  aa{           unsigned char b1:5;          unsigned  char b2:5;          unsigned char b3:5;          unsigned char b4:5;           unsigned char b5:5;  }aa;/*sizeof (AA)  = 5*/       typedef struct  bb {          unsigned  int b1:5;          unsigned int b2:5;           unsigned int b3:5;           unsigned int b4:5;           Unsigned int b5:5;  }bb;/*sizeof (BB)  = 4*/      typedef struct   CC {           int b1:1;            int :2;//No impact             int b3:3;           int b4:2;            int b5:3;            short b6:4;           int b7:1;            }cc; /*sizeof (CC)  = 12*/   

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.