The documentation is either poorly written or my command of the English as a foreign language is not up to par with it.
// make a nice 16 align macro
#ifndef ALIGN16
#define ALIGN16 __declspec(align(16))
#endif
// align the structure
struct ALIGN16 CB {
ALIGN16 bool m1; // and
ALIGN16 int m2; // align
ALIGN16 int m3; // each
ALIGN16 short m4; // element
};
// now it performs as expected
printf("sizeof(CB) %d\r\n", sizeof(CB));
CB vCb;
printf("CB: %p, %%%d\r\n", &vCb, (UINT_PTR)&vCb % 16);
printf("CB.m1: %p, %%%d\r\n", &vCb.m1, (UINT_PTR)&vCb.m1 % 16);
printf("CB.m2: %p, %%%d\r\n", &vCb.m2, (UINT_PTR)&vCb.m2 % 16);
printf("CB.m3: %p, %%%d\r\n", &vCb.m3, (UINT_PTR)&vCb.m3 % 16);
printf("CB.m4: %p, %%%d\r\n", &vCb.m4, (UINT_PTR)&vCb.m4 % 16);
The __declspec(align(#))
only affects the alignment of the structure and the sizeof()
, NOT each of the members in it. If you want each property to be aligned, you need to specify the alignment at member level.
I also initially assumed that a struct-level __declspec(align())
affects it and it's members but it does not. So if you want per member alignment, you need to be specific.