Observe that in the array of ExtraS objects, the s.total, s.a, and s.b are always four-byte aligned. The effect is more noticeable if you have an array of these objects. Similarly, there is no trail padding at the end of the structure because the P is byte-aligned and therefore does not need to be kept at a particular alignment in the case of an array of ExtraP objects. This ensures that an array of ExtraS structures will properly align all of the embedded S objects.īy comparison, the ExtraP structure starts out the same way, with a single char, but this time, there is no padding before the P because the P is byte-aligned. The ExtraS structure starts with a char, then adds three bytes of padding, followed by the S structure, then another char, and three more bytes of padding to bring the entire structure back up to 4-byte alignment. struct ExtraSĮven though the structures S and P have the same layout, the difference in alignment means that the structures ExtraS and ExtraP end up quite different. In this case, the #pragma pack(1) declares that the structure P can itself be placed at any byte boundary, instead of requiring it to be placed on a 4-byte boundary. When you use #pragma pack(1), this changes the default structure packing to byte packing, removing all padding bytes normally inserted to preserve alignment.Ĭonsider these two structures: // no #pragma pack in effect.īoth structures have identical layouts because the members are already at their natural alignment, Therefore, you would expect these two structures to be equivalent.Ĭhanging the default structure packing has another consequence: It changes the alignment of the structure itself.