Sizeof of struct, sizeof struct

Source: Internet
Author: User
Tags float double

Sizeof of struct, sizeof struct

First, there are several rules:

1. the offset between a struct member and a struct member is an integer multiple of the maximum simple type (memory usage) contained by the member (if the member is a struct, it is necessary to Recursively search for its simple type. The simple type is char short int float double, long)

For example, struct a1 {

Char a [5];

Int B;

} Aa;

Struct a2 {

Double;

Char B;

A1 c;

Char d;

} Bb;

In this example, the offset value of aa. B relative to aa is an integer multiple of int, so the Offset Value of aa. B is 8, and aa. a is filled with three bytes;

Bb in a2. the offset value of c is an integer multiple of the largest simple type contained in a1. The largest simple type contained in a1 is int, so bb. the offset value of c is a multiple of 4, so bb. the offset value of c is 12, the offset value of char type is a multiple of 1, and the double offset value is a multiple of 8. Length of long according to the specification, sizeof (long)> = sizeof (int), My 64-bit machine, vc2005, found int = long = 4 bytes, the 64-bit data type must use longlong or another type defined by windows.

2. the final size of the struct also needs to be filled with bytes at the end of the struct so that the struct size is an integer multiple of the maximum simple type of the struct (if you need to recursively query simple types, you need to recursively retrieve the maximum simple type ).

In the above example, the size of bb must be an integer multiple of 8. The size of aa is 12, and the starting position of a1 members in the struct should be an integer multiple of 4. In this way, the size of a2 is 8 + 1 + offset (bb. c) + sizeof (a1) + sizeof (d) + padding = 12 + sizeof (a1) + 1 + padding = 25 + padding = a multiple of 8, so sizeof (a2) it should be rounded to 32, and the last seven bytes are filled.

3. the union type is similar. The starting offset of the union member (this refers to the starting offset of the union member when the union is a member variable of the composite structure, relative to the composite) it must also be aligned to an integer multiple of the maximum simple type contained in the member, and the final size of the union must be equal to an integer multiple of the maximum simple type.

For example, union b1 {

Char a [5];

Int B;

};

Struct b2 {

Char a [3];

B1 B;

Char c;

};

Then the b1 size is 8 (the final size must be aligned to an integer multiple of the int type). The offset value of B in b2 should be a multiple of the maximum simple type of B Sub-members, that is to say, the offset of B is a multiple of 4, so the offset of B is 4, the size of b2 is 12, and the end of member c of b2 must be supplemented with three bytes, ensure that b2 is an integer multiple of the maximum simple type among its members.

4. if # pragma pack (n) is added, here n can only be 1, 2, 4, 8, 16... then, the previous calculation of "Maximum simple type" will be changed to the "Maximum simple type" and the minimum value of n.

For example

# Pragma push // Save the current alignment Value

# Pragma pack (4)

Struct testT

{

Char a1;

Double a2;

Char a3;

};

# Pragma pop // restore the alignment of memory during compilation

Then, calculate sizeof (testT) = 16. The default value is 24. Here, the a2 offset is not a multiple of 8, but is min (sizeof (dobule), 4) = 4, so the a2 offset is 4. It can also be seen from this that since the maximum simple type is double = 8 or 64-bit longlong = 8, n> = 16 is meaningless (generally, unless you have a larger simple type, 16-byte super cpu ...)

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.