How to calculate sizeof (struct) in C ++?

Source: Internet
Author: User

Copyright belongs to the original author. I am only a typographical engineer.

1. sizeoFStructure of applications

See the following structure:

 
StructMystruct
{
DoubleDda1;
CharDDA;
IntType;
};

What will happen if sizeof is used for the structure mystruct? SizeoF(Mystruct) What is it? You may ask:

Sizeof (mystruct) = sizeof (double) sizeof (char) sizeoF(INT) = 13

But when you test the above structure in VC, you will find sizeoF(Mystruct) is 16. Do you know why such a result is obtained in VC?

Actually, this is a special processing of variable storage by VC. To increase the CPU storage speed, VC performs "alignment" on the starting addresses of some variables. By default, VC specifies that the offset of the starting address of each member variable to the starting address of the structure must be a multiple of the bytes occupied by the variable type. Common alignment types are listed below (vc6.0, 32-bit system ).

Type
Alignment (offset of the starting address of the variable to the starting address of the structure)

Char
The offset must be sizeo.F(Char) is a multiple of 1

Int
The offset must be sizeo.F(INT) is a multiple of 4.

FLoat
The offset must be sizeof (FLoat) is a multiple of 4.

Double
The offset must be sizeo.F(Double) is a multiple of 8.
 

Short
The offset must be sizeo.F(Short) is a multiple of 2

When each member variable is stored, the space is requested in sequence based on the order in which the structure appears, and the positions are adjusted according to the alignment above. The vacant byte VC is automatically filled. At the same time, to ensure that the size of the structure is a multiple of the number of byte boundaries (that is, the number of bytes occupied by the Type occupying the maximum space in the structure, therefore, after applying for space for the last member variable, the vacant bytes will be automatically filled as needed.

The following uses the previous example to illustrate how VC stores the structure.

  struct   mystruct 
{< br> double dda1;
char DDA;
int type;
};

When allocating space for the above structure, VC allocates space for the first member dda1 according to the sequence and alignment of the member variables, the starting address is the same as the starting address of the structure (the offset 0 is just a multiple of sizeof (double). This member variable occupies sizeof (double) =8Next, allocate space for the DDA of the second member. the offset of the starting address of the structure for the next address that can be configured is 8, which is a multiple of sizeof (char, therefore, the DDA is stored in an alignment where the offset is 8. This member variable occupies sizeo.F(Char) =1Bytes. Next, allocate space for the third member type.The offset of the next allocable address to the starting address of the structure is 9 (8 + 1), not sizeoF(INT) = a multiple of 4To meet the offset constraint of alignment, VC automatically fills3Bytes (these three bytes do not include anything). At this time, the next address that can be allocated is 12 (8 + 1 + 3) for the starting address of the structure ), it is just a multiple of sizeof (INT) = 4, so the type is stored in the place where the offset is 12, and the member variable occupies sizeof (INT) =4At this time, the member variables of the entire structure have been allocated space, the total space occupied is: 8 1 3 4 = 16, it is just a multiple of the number of byte boundary values of the structure (that is, the byte sizeof (double) = 8) occupied by the Type occupying the maximum space in the structure), so no vacant bytes need to be filled. Therefore, the size of the entire structure is sizeo.F(Mystruct) = 8 + 1 + 3 + 4 = 16. Among them, three bytes are automatically filled by VC, and nothing makes sense.

Next, let's take another example to change the position of the member variable of mystruct above to the following:
StructMystruct
{
CharDDA;
DoubleDda1;
IntType;
};

How much space does this structure occupy? In the vc6.0 environment, sizeo can be obtained.F(Mystruc) is 24. Based on the space allocation principles mentioned above, we will analyze how VC allocates space for the above structure. (Simple description)

 Struct  Mystruct
{
Char DDA; // The offset is 0. alignment is satisfied, and DDA occupies1Bytes;  
  Double Dda1; // The offset of the next available address is 1, not a multiple of sizeof (double) = 8.7Offset in bytes // Changes to 8 (alignment is satisfied). Therefore, VC automatically fills in 7 bytes, and dda1 is stored on the address with the offset of 8, It occupies8Bytes.
  Int Type; // The offset of the next available address is 16, which is a multiple of sizeof (INT) = 4, // The alignment of int is satisfied, so VC does not need to be automatically filled. type is stored on the address with the offset of 16, which occupies4Bytes.  
}; // All member variables are allocated space. The total size of the space is 1 + 7 + 8 + 4 = 20, not the number of node boundaries of the structure. (That is, the type occupied by the largest space in the Structure // The number of bytes, multiple of sizeof (double) = 8, Therefore, four bytes must be filled to meet the requirement that the structure size is a multiple of sizeof (double) = 8.

the total size of the structure is sizeo F (mystruc) 1 + 7 + 8 + 4 + 4 = 24. Among them, 7 + 4 = 11 bytes are automatically filled by VC, and nothing makes sense.

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.