public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Arrays and Alignment
@ 2003-09-23 19:29 Michael N. Moran
  2003-09-23 19:40 ` Andreas Schwab
  0 siblings, 1 reply; 11+ messages in thread
From: Michael N. Moran @ 2003-09-23 19:29 UTC (permalink / raw)
  To: gcc

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



^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: Arrays and Alignment
@ 2003-09-23 20:44 Richard Kenner
  2003-09-23 21:05 ` Gabriel Dos Reis
  0 siblings, 1 reply; 11+ messages in thread
From: Richard Kenner @ 2003-09-23 20:44 UTC (permalink / raw)
  To: gdr; +Cc: gcc

| > typedef unsigned char achar __attribute__ ((__aligned__(32)));
| > achar           a[2];

| > __alignof__(a[1]):1

I think this is wrong: it should be 32.

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

Yes, but not in this direction!  An object cannot be *less* aligned than
it's type.  I argued for being able to represent an object that was *more*
aligned than its type.

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2003-09-24  8:48 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-23 19:29 Arrays and Alignment Michael N. Moran
2003-09-23 19:40 ` Andreas Schwab
2003-09-23 19:50   ` Michael N. Moran
2003-09-23 21:53     ` Neil Booth
2003-09-23 22:10       ` Gabriel Dos Reis
2003-09-23 22:17         ` Neil Booth
2003-09-23 22:26           ` Gabriel Dos Reis
2003-09-24  9:57           ` Andreas Schwab
2003-09-23 19:56   ` Gabriel Dos Reis
2003-09-23 20:44 Richard Kenner
2003-09-23 21:05 ` Gabriel Dos Reis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).