Go How does sizeof (struct) in C + + calculate?

Source: Internet
Author: User

Copyright belongs to the original author, I just typesetting.

1. The application of sizeof in the structure of the situation

Take a look at the following structure:

struct MYSTRUCT
{
Double dda1;
Char DDA;
int type;
};



What happens when you use sizeof for structural mystruct? How much is sizeof (MYSTRUCT)? Perhaps you would ask:

sizeof (MyStruct) =sizeof (double) sizeof (char) sizeof (int) =13

However, when you test the size of the structure in the VC, you will find that sizeof (MYSTRUCT) is 16. Do you know why the VC will produce such a result?

In fact, this is a VC on the variable storage of a special treatment. In order to improve the storage speed of the CPU, VC has made the "alignment" processing for the starting address of some variables. By default, the VC specifies that the offset of the starting address for each member variable to be stored relative to the start address of the structure must be a multiple of the number of bytes occupied by the variable's type. The following is a list of common types of alignments (vc6.0,32-bit systems).

Type
Alignment (the offset at which the variable holds the starting address relative to the structure's start address)

Char
The offset must be a multiple of sizeof (char), or 1

Int
The offset must be a multiple of sizeof (int), or 4

Float
The offset must be a multiple of sizeof (float), or 4

Double
The offset must be a multiple of sizeof (double), or 8


Short
The offset must be a multiple of sizeof (short), or 2


Each member variable applies space in the order in which it appears in the structure, and adjusts the position according to the alignment above, and the empty byte VC is automatically populated. At the same time VC to ensure that the size of the structure of the structure of the number of bytes (that is, the structure of the maximum space occupied by the type of the number of bytes), so after the last member variable to request space, also automatically fill the empty bytes as needed.

The following example to illustrate how VC exactly how to store the structure.

struct MYSTRUCT
{
Double dda1;
Char DDA;
int type;
};


When allocating space for the above structure, the VC allocates space for the first member DDA1 according to the order and alignment of the member variables, the starting address is the same as the starting address of the structure (just 0 is exactly the multiple of sizeof (double), which takes up sizeof ( Double) = 8 bytes; Then allocate space for the second member DDA, at which point the next assignable address has an offset of 8 for the starting address of the struct, a multiple of sizeof (char), so the DDA is stored at an offset of 8 where the alignment is satisfied, and the member variable occupies sizeof (char) = 1 bytes; Next assigns a space to the third member type, at which point the next assignable address has an offset of 9 (8+1) from the start address of the struct, not a multiple of the sizeof (int) =4. In order to satisfy the constraint of the alignment on the offset, the VC automatically fills 3 bytes (this three bytes did not put anything), then the next assignable address for the structure of the start address offset of 12 (8+1+3), just a multiple of sizeof (int) =4, So the type is stored at an offset of 12, the member variable accounts for sizeof (int) = 4 bytes, then the entire structure of the member variable has been allocated space, the total amount of space occupied is: 8 1 3 4 = 16, which is just a multiple of the number of byte boundaries of the structure (that is, the byte sizeof (double) =8) of the type that occupies the maximum space in the structure, so there are no empty bytes to fill. So the size of the whole structure is: sizeof (mystruct) =8+1+3+4=16, which has 3 bytes is the VC automatically filled, did not put anything meaningful.

In the following example, swap the position of the member variable of the above mystruct to make it the following situation:
struct MYSTRUCT
{
Char DDA;
Double dda1;
int type;
};


How much space does this structure occupy? In the VC6.0 environment, sizeof (MYSTRUC) can be obtained as 24. According to some principles of allocating space mentioned above, this paper analyzes how VC allocates space for the above structure. (Simple description)

struct MYSTRUCT
{
Char DDA; The offset is 0, the alignment is satisfied, and the DDA occupies 1 bytes;
Double dda1;   The offset of the next available address is 1, not a multiple of the sizeof (double) =8, 7 bytes is required to make the offset//to 8 (for alignment), so the VC automatically fills 7 bytes, and the dda1 is stored at an offset of 8, which occupies 8 bytes. int type;//The offset of the next available address is 16, is the multiple of sizeof (int) =4,//the alignment of int is satisfied, so no VC autofill is required, and the type is stored at an offset of 16, which occupies 4 bytes.
};///All member variables are allocated space, the total size of the space is 1+7+8+4=20, not the number of section boundaries of the structure (i.e. the number of bytes that occupy the maximum space in the structure is occupied//of the Byte sizeof (double) =8), so you need to populate 4 bytes, To meet the size of the structure as a multiple of sizeof (double) =8.



So the total size of the structure is: sizeof (MYSTRUC) is 1+7+8+4+4=24. One of the total 7+4=11 bytes is the VC automatically filled, not put anything meaningful.

Go How does sizeof (struct) in C + + calculate?

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.