A Brief Look at the Mathematics of Structure Packing

It's common knowledge that the memory layout of a structure in C can change depending on the order its members were declared in. For example, on my x86-64 processor, sizeof(Foo) is not equal to sizeof(Bar) , even though they effectively have the same members. You can try it out yourself on Godbolt . struct Foo { char firstChar ; double firstDouble ; char secondChar ; } ; struct Bar { double…