Calculation of struct size in C

Source: Internet
Author: User

Recently I have studied the calculation of the structure size in C language. Based on my understanding and references,

I want to make a summary.

First, we will introduce a related concept-offset. Offset refers to the address and struct of the members in the struct variable.

Variable address difference. The size of the struct is equal to the offset of the last member and the size of the last member.

Due to address alignment requirements when storing variables, the compiler will follow two principles during program Compilation:

1. The member offset 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.

 

This is a saying, and there is also a saying:

 

The default byte alignment of a struct generally meets three criteria:

1) The first address of the struct variable can be divisible by the size of its widest basic type member;

2) The offset (offset) of each member of the struct to the first address of the struct is an integer multiple of the member size,

If necessary, the compiler will add the padding byte (internal adding) between the members );

3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will

Add the padding byte after a member.

 

I wrote three structs and tested them. The Code is as follows:

 

The output result is:

12

8

20

 

First look at the first struct. the offset of the first member a is 0. The offset of the second member B 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 c is the offset of the second member plus the size of the second Member (4 + 1), and its value is 5. 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 and the size of the last member. Offset 8 plus the size of the last member c itself, that is, 9 cannot be divisible by the size of the widest basic type member, so here the compiler will fill in 3 bytes at the end to get 12.

As for the second struct, the size is equal to the offset of the last member plus its size. The size calculated in the above example is 8, which meets the requirements.

According to the above calculation rules, both struct 1 and struct 2 can calculate the same result as the output, but struct 3 does not understand.

 

Size of the Offset-filled byte Member

0 4

4 1

5 1 2

8

16 4

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.