This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Arrays and Alignment
- From: Andreas Schwab <schwab at suse dot de>
- To: "Michael N. Moran" <mnmoran at bellsouth dot net>
- Cc: gcc <gcc at gcc dot gnu dot org>
- Date: Tue, 23 Sep 2003 21:29:47 +0200
- Subject: Re: Arrays and Alignment
- References: <3F709E2D.3030400@bellsouth.net>
"Michael N. Moran" <mnmoran@bellsouth.net> writes:
> 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
You can't have both. That would require to put padding between array
elements, which is not allowed (unlike structure members).
Andreas.
--
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."