Analysis of the C/C ++ sizeof function -- solving the problem of sizeof finding the struct size

Source: Internet
Author: User

Analysis of the C/C ++ sizeof function -- solving the problem of sizeof finding the struct size
The memory occupied by different data types in C/C ++ is 32-bit, 64-bit, char 1 1 int 4, most 4, A few 8 short 2 2 long 4 8 float 4 4 double 8 8 pointers 4 8 (in bytes) struct (struct): relatively complex, alignment problems. Union: the longest of all members. Enumeration (enum): based on the data type. The sizeof operator used to calculate the size of a single-layer struct can calculate the size of a given type. For 32-bit systems, sizeof (char) = 1; sizeof (int) = 4. The size of the basic data type is well calculated. Let's take a look at how to calculate the size of the constructed data type. There are three types of constructed data in C language: array, struct, and shared body. An array is a set of elements of the same type. As long as the size of a single element is calculated, the space occupied by the entire array is equal to the size of the base element multiplied by the number of elements. The members in the struct can be of different data types. The members are stored in sequential memory according to the defined sequence. Unlike arrays, the size of a struct is not a simple addition of the size of all members. You must consider the address alignment problem when the system stores the struct variable. Take a look at the following struct: struct stu1 {int I; char c; int j ;}; use sizeof to calculate the size of the struct. The value is 12. Int occupies 4 bytes, char occupies 1 byte, and the result should be 9 bytes. Why? First, we will introduce a related concept-offset. Offset refers to the difference between the address of the member in the struct variable and the address of the struct variable. The size of the struct is equal to the offset of the last member and the size of the last member. Obviously, the address of the first Member in the struct variable is the first address of the struct variable. Therefore, the offset of the first Member I is 0. The offset of the second member c is the offset of the first member plus the size of the first Member (0 + 4), and its value is 4; the offset of the third member j is the offset of the second member plus the size of the second Member (4 + 1), and its value is 5. However, in practice, address alignment is required when storing variables, and the compiler will follow two principles during program Compilation: (1) the offset of a member in the struct variable must be an integer multiple of the member size (0 is considered an integer multiple of any number) (2) the struct size must be an integer multiple of the size of all members, that is, the public multiple of all members. In the preceding example, the offsets of the first two members meet the requirements, but the offset of the third member is 5, which is not an integer multiple of its own (int) size. During processing, the compiler adds three NULL bytes after the second member to change the offset of the third member to 8. The size of the struct is equal to the offset of the last member plus its size. The size calculated in the preceding example is 12, which meets the requirements. Let's look at another example: struct stu2 {int k; short t ;}; the offset of member k is 0; the offset of member t is 4, and no adjustment is required. The calculated size is 6, which is obviously not an integer multiple of the size of the member k. Therefore, the compiler will add two bytes after Member t to change the size of the struct to 8 to meet the second requirement. It can be seen that the structure type needs to take into account the situation of byte alignment, different order will affect the size of the structure. Compare the following two definitions:

struct stu3  {          char c1;          int i;         char c2;  }  struct stu4  {         char c1;         char c2;         int i;   }  

 

Although stu3 and stu4 have the same members, the value of sizeof (struct stu3) is 12, and the value of sizeof (struct stu4) is 8. Sizeof calculates the size of the nested struct. For the nested struct, expand it. When sizeof is obtained for the struct, the preceding two principles become: (1) the offset of the first member of the expanded struct should be an integer multiple of the largest member of the expanded struct. (2) The struct size must be an integer multiple of the size of all members. Here, all Members calculate the expanded member rather than the nested struct as a whole. See the following example:
struct stu5  {        short i;        struct         {             char c;             int j;        } ss;         int k;  }   

 

The offset of the ss. c Member of the stu5 struct should be 4, not 2. The size of the entire struct should be 16. The following code Test Principle 2:
struct stu5  {        char i;        struct         {             char c;             int j;        } ss;         char a;        char b;        char d;        char e;        char f;  }  

 

The structure ss calculates the occupied space as 8, while stu5's sizeof is 20, not an integer multiple of 8. This indicates that when sizeof (stu5) is calculated, expand the nested struct ss so that the largest member of stu5 is ss. j. It occupies 4 bytes and 20 is an integer multiple of 4. If we regard ss as a whole, the result should be 24. Another special example is that a struct contains an array. Its sizeof should be expanded like a nested struct, as shown in the following example:
struct ss  {      float f;      char p;      int adf[3];  };   

 

The value is 20. Float occupies 4 bytes. The offset to char p is 4. p occupies 1 byte. The offset to int adf [3] is 5, which is extended to an integer multiple of int, instead of an integer multiple of int adf [3], the offset is changed to 8 instead of 12. The result is 8 + 12 = 20, which is an integer multiple of the maximum member float or int size. How to allocate space to struct variables is determined by the compiler. The above situation is for GCC in Linux. The same is true for VC platforms in Windows. Other platforms may have different processing methods.

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.