Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 64-bit struct size varies with contained data type?

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.

like image 497
Erika Electra Avatar asked Dec 16 '22 23:12

Erika Electra


1 Answers

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 pix1 has alignment 1, because it only has chars, but pix2 has alignment 2 from the short member. The alignment of pix4 then gets the alignment from the pixels member, 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 pixels is 24, but then there is 1-byte mask. In the first case the alignment is 1, so 25 is multiple of it and sizeof(pix4) is 25, but in the second the alignment is 2, so sizeof(pix4) has to be rounded up to next even number, 26.

This is the same on all platforms.

like image 74
Jan Hudec Avatar answered Feb 23 '23 14:02

Jan Hudec