This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Arrays and Alignment


Andreas Schwab wrote:

"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,

Thanks for the instantaneous reply :-)

I must confess, however, that I don't understand what you mean
by "both". Do you mean (1) array alignment and (2) array element
alignment? In this case it would seem that the type "achar"
requires an alignment of 32bytes. Why would that not extend
to the elements of the array?

mike



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]