The size of the structures is the same, but their alignment requirements are different.
Alignment of the structure is the maximum alignment of all its members. So pix1
has alignment 1 because it only has characters, but pix2
has alignment 2 from the short term. Then alignment pix4
receives alignment from the pixels
member, so it is 1 in the first and 2 in the second case.
Now, so that all elements of the array are correctly aligned, the size of the structure is rounded to the next multiple alignment. In both cases, the size of pixels
is 24, but then there is a 1-byte mask
. In the first case, the alignment is 1, so 25 is a multiple of it, and sizeof(pix4)
is 25, and in the second, the alignment is 2, so sizeof(pix4)
needs to be rounded to the next even number, 26.
This is the same on all platforms.
source share