This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Arrays and Alignment
- From: "Michael N. Moran" <mnmoran at bellsouth dot net>
- To: gcc <gcc at gcc dot gnu dot org>
- Date: Tue, 23 Sep 2003 15:25:33 -0400
- Subject: Arrays and Alignment
I am declaring an array of an aligned type. However
it would appear that when used in an array, the alignment
is not maintained as I would expect. Here's a test-case.
#include <stdio.h>
typedef unsigned char achar __attribute__ ((__aligned__(32)));
achar a[2];
int main(int argc,char* argv[]){
printf("__alignof__(achar):%u\n",__alignof__(achar));
printf("__alignof__(a[1]):%u\n",__alignof__(a[1]));
printf("&a: %8.8lX\n",(unsigned long)&a);
printf("sizeof(a):%u\n",sizeof(a));
printf("sizeof(a[0]):%u\n",sizeof(a[0]));
printf("&a[0]: %8.8lX\n",(unsigned long)&a[0]);
printf("&a[1]: %8.8lX\n",(unsigned long)&a[1]);
return 0;
}
% gcc -o main main.c ; main
__alignof__(achar):32
__alignof__(a[1]):1
&a: 08049740
sizeof(a):32
sizeof(a[0]):1
&a[0]: 08049740
&a[1]: 08049741 <<<<<<<<<<<< I expected this to be 08049760
% gcc --version
gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
I expected the second element of the array to be aligned
as well as the first, since the alignment of the type is 32.
The Beatles were wrong: 1 & 1 & 1 is 1