#include #ifdef __GNUC__ #define MS_ALIGN(N) __declspec(aligned(N)) #define GNU_ALIGN(N) #else #define GNU_ALIGN(N) #define MS_ALIGN(N) __declspec(align(N)) #endif static int diff_ptr (const void *p1, const void *p2) { const char *c1 = (const char *) p1; const char *c2 = (const char *) p2; return (int) (c2 - c1); } static int check_ptr (const void *base, const void *off, int expect) { int v = diff_ptr (base, off); if (v == expect) return 1; printf (" expect %d, but we got %d", expect, v); return 0; } MS_ALIGN(1) struct sP1_1 { char a; unsigned char b; MS_ALIGN(1) short c GNU_ALIGN(1); } GNU_ALIGN(1); MS_ALIGN(1) struct sP2_1 { char a; unsigned char b; MS_ALIGN(2) short c GNU_ALIGN(2); } GNU_ALIGN(1); MS_ALIGN(1) struct sP4_1 { char a; unsigned char b; MS_ALIGN(4) short c GNU_ALIGN(4); } GNU_ALIGN(1); MS_ALIGN(1) struct sP8_1 { char a; unsigned char b; MS_ALIGN(8) short c GNU_ALIGN(8); } GNU_ALIGN(1); #define chk(STR, FIELD, EXPECT) \ { struct STR a##STR; \ if (!check_ptr (&a##STR, &a##STR.FIELD, EXPECT)) \ printf (" %s (%s - .) == %d: failed\n", "a" #STR, #FIELD, EXPECT); \ } #define siz(STR, SZ) \ if (sizeof (struct STR) != SZ) \ printf ("sizeof(%s): is %d and not equal to expected %d\n", #STR, (int) sizeof (struct STR), SZ); \ int main() { chk(sP1_1, c, 2); siz(sP1_1, 4); chk(sP2_1, c, 2); siz(sP2_1, 4); chk(sP4_1, c, 4); siz(sP4_1, 8); chk(sP8_1, c, 8); siz(sP8_1, 16); return 0; }