Hmm…. why does this struct allocate 16 Bytes when the data types in it should only require 9 Bytes?

Members of a structure must appear at the correct byte boundaries. While char variables can be aligned at any byte boundary, shorts can only be addressed at even byte boundaries, and longs only at every 4th byte.

Because of this the compiler automatically adds padding bytes to the structure, so that the members appear at the correct memory location. In our example the compiler adds one padding byte after the c1-member, three padding bytes after the c2-member and fills in the remaining bytes after the c3-member.

You can insert padding-members so you can see how much memory a struct will require, just by looking at your source:

Usually, you don’t need to put in the padding members in the struct, as the compiler will take care of it. But if you are used to do it, and you have to add additional members later on it’s more easy for you to put them at the correct location. In our example you can add three additional chars to the struct and it still only requires 12 Bytes.