Calculation of struct size and space optimization-Bit Field

Source: Internet
Author: User

/*************************************** ******************************* Author: samson * Date: 09/21/2013 * Test platform: * #1 SMP Debian 3.7.2-0 + kali8 * gcc (Debian 4.7.2-5) 4.7.2 ***************************************: http://blog.csdn.net/yygydjkthh/article/details/11850735 The length of a struct field with a bit field is calculated as follows, the length of the struct consisting of bitfield calculation and basic type values is not an algorithm. Test code and Result: # include <stdio. h> # include <stdlib. h> typedef struct BitArea1 {char c: 4; int l: 30;} BITAREA1; // The length of the above struct is 4bit + 30bit = 34bit, it is larger than a single byte (32bit), and because: 2nd of the alignment rules: the alignment value of the structure or class itself: the value with the largest alignment value of its members. Therefore, it should be aligned according to the four bytes of the int type. Therefore, the 34bit complement alignment occupies 8 bytes. If the sum of the two fields is less than or equal to 32bit, the space occupied by this struct is 4 Bytes: typedef struct BitArea1 {char c: 4; int l: 20;} BITAREA1; likewise, if the int type is modified to the short type, when the number of bits added to the two fields is smaller than or equal to sizeof (short) = 2 Byte = 16bit, the occupied space should be 2 bytes: typedef struct BitArea1 {char c: 4; short l: 10;} BITAREA1; if the number of bits added to the two fields is greater than 2 Byte = 16 bit, the space occupied is 4 bytes; typedef struct BitArea1 {char c: 4; short l: 13;} BITAREA1; typedef struct BitArea {Int a: 1; char B: 1; int c; char d: 3; char e: 5; int f; char g: 3;} BITAREA; // The length of the above struct should be 2 bits for a and B, and because a is of the int type and C is of the int type, according to the first and second rules, then a + B + c = 4 + 4 = 8, d + e is exactly 8 bit, And because f is int, d + e + f = 4 + 4 = 8, g is 3bit, 8 + 8 + 1 = 17, and according to the first rule: the alignment value of the struct or class: the value of the biggest alignment value of its members. The final result is 20; typedef struct BitAreaBetter {char B: 1; char d: 3; char e: 5; char g: 3; int c; int f; int: 1;} BITAREABETTER; // The length of the above struct should be 4 + 4 + 4 + 4 = 16 according to the previous conclusion; because C of the first four char Types is int type, the length of the two bytes added up by the first four char types should be supplemented by 4 bytes; typedef struct BitAreaBest {char B: 1; char d: 3; char e: 5; char g: 3; int a: 1; int c; int f;} BITAREABEST; // according to the previous summary, the length of the above struct can also be 4 + 4 + 4 = 12; the total space occupied by the first four char Types plus an int bit is smaller than the size of an int type. Therefore, the total space occupied is 4 bytes in the first rule, the last two values of the int type are 4 bytes. // The Optimization Method for the struct with bits field is: Put the bits field into one partition as much as possible; typedef struct BitAreaBest1 {int a: 1; int c; int f; char B: 1; char d: 3; char e: 5; char g: 3;} BITAREABEST1; // The length of the above struct is actually the same as that of BITAREABETTER, but the sequence is adjusted. int main () {int len, lenBest, lenBett, len01, lenBest01; BITAREA BitArea; BITAREA1 BitArea1; commandid; BITAREABEST BitAreaBest; commandid; len = sizeof (BitArea); lenBett = sizeof (bytes); lenBest = sizeof (BitAreaBest); len01 = sizeof (BitArea1 ); lenBest01 = sizeof (BitAreaBest01); printf ("BitArea1 len is % d BITAREABETTER len is % d BitAreaBest len is % d len01 is % d BitAreaBest01 len is % d \ n", len, lenBett, lenBest, len01, lenBest01); return 0;} root @ quieter:/usr/local/samsonfile/yygytest #. /. out BitArea1 len is 20 BITAREABETTER len is 16 BitAreaBest len is 12 len01 is 8 BitAreaBest01 len is 16

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.