/* * Testcase for a possible compiler bug: * structure with flexible array member. * offsetof(struct,flexible_member) != sizeof(struct). * * That seems to be a violation of 6.7.2.1, constraint 16 of the C99 * standard: * "First, the size of the structure shall be equal to the offset of the * last element of an otherwise identical structure that replaces the * flexible array member with an array of unspecified length. */ #include /* * structure: sizeof() = 4. * alignment: 2, due to the short. */ struct pollfd { short fd; char events; char revents; }; /* * sizeof: 12 * alignment: 4, due to the int. */ struct n2 { /* offset 0 */ int m; /* offset 4 */ char n; /* one byte padding, 'struct pollfd' requires 2 byte alignment */ /* offset 6 */ struct pollfd a[1]; /* offset 10: two bytes padding for 4 byte alignment, * required for the int in this structure */ }; /* * sizeof: 8. * XXX * Is this correct? * offsetof(struct n1, a) is 6 * --> offsetof(struct n1, a) != sizeof(struct n1) * XXX */ struct n1 { /* offset 0 */ int m; /* offset 4 */ char n; /* one byte padding, 'struct pollfd' required 2 byte alignment */ /* offset 6 */ struct pollfd a[]; }; #define offsetof(a,b) \ (int)&((a*)NULL)->b int main(void) { printf("pollfd: sizeof: %d.\n", sizeof(struct pollfd)); printf("fix: sizeof: %d, offsetof: %d.\n", sizeof(struct n2), offsetof(struct n2, a)); printf("var: sizeof: %d, offsetof: %d.\n", sizeof(struct n1), offsetof(struct n1, a)); return 0; }