c++ - Windows 64-bit struct size varies with contained data type? -
i have 2 different data structs should, in principle, have same size, , i'm wondering why 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, group 4 of these pixels together, such:
struct pix4 { pix1 pixels[4]; // or pix2 pixels[4] unsigned char mask; }; ...but turns out size of such grouping changes, according sizeof(pix4), depending on whether use pix1 or pix2. individual sizeof(pix1) == sizeof(pix2), confused why grouping quartets of pixels changes size. care because easier write programs short 2 unsigned chars, it's costing me 0.25 bytes per pixel.
i'm not sure if architecture specific, haven't tested on other types of machines. alignment? need worry about, or can proceed short implementation?
thanks in advance.
the size of structures same, alignment requirement different.
alignment of structure maximum of alignment of members. pix1 has alignment 1, because has chars, pix2 has alignment 2 short member. alignment of pix4 gets alignment pixels member, it's 1 in first , 2 in second case.
now ensure members of array aligned, size of structure rounded next multiple of it's alignment. in both cases size of pixels 24, there 1-byte mask. in first case alignment 1, 25 multiple of , sizeof(pix4) 25, in second alignment 2, sizeof(pix4) has rounded next number, 26.
this same on platforms.
Comments
Post a Comment