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 <schwab@suse.de> writes:

| "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).

then that is a hole in GCC's type system.  A hole that needs fixing.
There seems to be two issues being confused here:
  (1) type alignement; and
  (2) object alignment.

If a type T is declared to have an alignment N, then given  a
pointer p  to a T,  ++p should be a multiple of N.  That is possible
only it you make sizeof(T) a multiple of N.  Which implies some padding.

Note however that an object may be defined to have a user-supplied
alignment without implying that its type has the same alignment.  

I remember Kenner argued for that distinction (a while ago).  I can't
recall what the conclusion of the debate was; but at any rate
Michael's expectations are rasonable, and I think they should be met,
i.e. we have a bug.

-- Gaby


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