A small analysis of the structure body storage

Source: Internet
Author: User

Alignment: Each member variable in the struct is stored as a multiple of the length of the memory space that the type occupies. More than 4 bytes by 4 bytes

Alignment is also: the number of bits of the previous type in the struct is determined by the type of variable that follows it!

Completion: In the structure of the storage space at the end of the structure of the largest member of the space occupied by the length of the space to fill in multiples. The count is 4 bytes greater than 4 bytes.

Completion: The last length of a struct is determined by the data type of the largest member variable in the struct, which must be an integral multiple of the latter.

For example:

1#include <stdio.h>2typedefstructTest {3     Charname[ -];4     intWord;5     Charbit;6     DoubleNumber ;7      ShortAge ;8 9 } mix;Ten  One intMain () { A mix Hello; -     intn =sizeof(n); -printf"%d \ n", n);//The result is a the     return 0; -}

As we have expected, the result is a, well, how could we get it? Now let us explain it:

Split-Step parsing:

1:NAME[18], Bytes 0-18->20

2:word, 4 bytes 20-24

3:bit, 1 bytes 24-25->32

4:number, 8 bytes 32-40

5:age, 2 bytes 40-42

6: The maximum number of bytes is 8 (the array is a char type, the number of bytes is 1), and the last struct body length is an integer multiple of the maximum number of bytes, so the complement to 48

A small analysis of the structure body storage

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.