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!