Deep understanding of class fill rules

Source: Internet
Author: User

The total size of the member variables in Struct/class does not simply add the size of the individual member variables, but takes into account the efficiency of the reads, sometimes adding padding bytes.
For example, some platforms read each time from the even address, if an int (assuming 32-bit system) if the location of the place where the even address begins, then a read cycle can be read out of the 32bit, and if it is stored at the beginning of the odd address, it takes 2 read cycles, The 32bit data can be obtained by piecing together the high and low bytes of the two read-out results.


  Fill rule: (1) The first address of a struct variable can be divisible by the size of its widest base type member;
(2) The offset of each member of the struct relative to the first address of the struct is an integer multiple of the member size, and if necessary the compiler adds padding bytes between the members
(3) The total size of the struct is an integer multiple of the size of the structure's widest base type member, and if necessary the compiler will add padding bytes after the last member.


Determining the size of a class is usually used in rules (2) and (3).
Example 1: Basic analysis

 class   Coo { char   C;     short   S;     int   I;  short   S2;};  

First, the offset of C is 0, and 0 is an integer multiple of 1, so it is not padded; s is offset by 1, and 1 is not an integer multiple of 2, so it is filled with 1;
The offset of I is 4, and 4 is an integer multiple of 4, so no padding is necessary; The offset of the S2 is 8, and 8 is an integer multiple of 2, so no padding is necessary.
Finally, in class COO, the widest base type is int, and the size is 4, while our class size is: 1 (+1) + 2 + 4 + 2 = 10, not an integer multiple of 4, so the last padding is 2.


Example 2: Empty class

class Coo {};

the size of the empty class is 1 . If the COO size is 0, the other empty classes cannot be distinguished.


Example 3: There are static members

class Coo {    staticint  i;};

< Span class= "Apple-tab-span" > < The size of the span class= "Apple-tab-span" category is 1. Static data members are not part of the instance, but are placed in the global data area.


Example 4: Class nesting

 class   Cooin { int   I;     short   S;  int   I2;};  class   coo{ char      C;    Cooin CI;  double   D;};  

< Span class= "Apple-tab-span" > < Span class= "Apple-tab-span" > first analysis was The size of the nested cooin is easily 12, and the widest data member size is 4. Then with CI as the demarcation point, the front size is 4 integer times, therefore 1 + 3, and the subsequent analysis of the
there is nothing special. Note that Cooin cannot be expanded within the COO as

 class   coo{ char   C;     int   I;     short   S;     int   I2;  double   D;};  

to treat Cooin as a whole, its transmission unit is 4, so CI before adding cooin, to ensure that is 4 of the integer times

Deep understanding of class fill rules

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.