http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47557
Summary: Effect of aligned attribute on arrays Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: akyr...@gmail.com The following test case demonstrates what the effect aligned on the size of arrays: typedef __attribute__((aligned(2))) struct { char a[3]; } T; unsigned x1 = sizeof(T); // sizeof is 3 unsigned x2 = sizeof(T[1]); // sizeof is 4 unsigned x3 = sizeof(T[2]); // sizeof is 6 unsigned x4 = sizeof(T[2][1]); // sizeof is 8 unsigned x5 = sizeof(T[1][2]); // sizeof is 6 The result is that type 'T' is not aligned by the array-of-T is. Padding arrays seems like a mistake, I couldn't find any mention in the standards about padding in arrays. C++ 5.3.3p2 (expr.sizeof) says: "When applied to an array, the result is the total number of bytes in the array. This implies that the size of an array of n elements is n times the size of an element." C99 6.5.3.4p6 (sizeof) implies the same with "Another use of the sizeof operator is to compute the number of elements in an array: sizeof array / sizeof array[0]" Note that in both standards, in those sections they specifically mention padding for classes/structures. IMO the reasonable thing is to fully respect the alignment of T and pad it to 4 bytes so that the elements in array-of-T are aligned, e.g. second element of array-of-T is not aligned as it is now. So, to sum up, I think the test case should be like this: typedef __attribute__((aligned(2))) struct { char a[3]; } T; unsigned x1 = sizeof(T); // sizeof is 4 unsigned x2 = sizeof(T[1]); // sizeof is 4 unsigned x3 = sizeof(T[2]); // sizeof is 8 unsigned x4 = sizeof(T[2][1]); // sizeof is 8 unsigned x5 = sizeof(T[1][2]); // sizeof is 8