I have two different data structs that should, in principle, have the same size, and I’m wondering why they do not.
struct pix1 {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
unsigned char y[2];
};
struct pix2 {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
unsigned short y;
};
Then, I group four of these pixels together, as such:
struct pix4 {
pix1 pixels[4]; // or pix2 pixels[4]
unsigned char mask;
};
…but it turns out that the size of such a grouping changes, according to sizeof(pix4), depending on whether I use pix1 or pix2. The individual sizeof(pix1) == sizeof(pix2), so I am confused as to why grouping quartets of pixels changes the size. I care because it is easier to write programs with the short than with the 2 unsigned chars, but it’s costing me 0.25 bytes per pixel.
I’m not sure if this architecture specific, as I haven’t tested on other types of machines. Could it be alignment? Is this something I need to worry about, or can I proceed with the short implementation?
Thanks for your help in advance.
The size of the structures is the same, but their alignment requirement is different.
Alignment of a structure is the maximum of alignment of all its members. So
pix1has alignment 1, because it only has chars, butpix2has alignment 2 from the short member. The alignment ofpix4then gets the alignment from thepixelsmember, so it’s 1 in the first and 2 in the second case.Now to ensure all members of an array are properly aligned, size of a structure is rounded up to next multiple of it’s alignment. In both cases the size of
pixelsis 24, but then there is 1-bytemask. In the first case the alignment is 1, so 25 is multiple of it andsizeof(pix4)is 25, but in the second the alignment is 2, sosizeof(pix4)has to be rounded up to next even number, 26.This is the same on all platforms.