[Daily C 3] struct byte alignment

Source: Internet
Author: User

Struct byte alignment for daily one-C sortingEvery day, I picked up a C-language shell, which grew up and formed thousands of miles.
Today's shell: struct is a collection of multiple correlated data, which is stored in the memory as a whole.So does struct A occupy 1 + 4 + 2 = 7 bytes of memory? 7 seems reasonable, but in fact, to be compatible with various hardware, the memory allocation of the struct adopts the specific byte alignment mode.1. Data Type alignment value: the value of sizeof.2. Structure alignment value: the alignment value of the maximum alignment type in the structure.3. The total size of the struct is multiplied by its alignment value. When necessary, the compiler automatically adds NULL bytes.4. structure elements distribute memory in sequence.Follow these rules:A starts at 0th bytes and occupies one byte. B starts at 4th bytes and occupies 4 bytes. c starts at 8th bytes and occupies 2 bytes. To make the total size of the struct a multiple of its alignment values, add two NULL bytes. The total size of the struct is 12.Have a nice day!


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.