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 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 19:56   ` Gabriel Dos Reis
  0 siblings, 2 replies; 11+ messages in thread
From: Andreas Schwab @ 2003-09-23 19:40 UTC (permalink / raw)
  To: Michael N. Moran; +Cc: gcc

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

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

* Re: Arrays and Alignment
  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 19:56   ` Gabriel Dos Reis
  1 sibling, 1 reply; 11+ messages in thread
From: Michael N. Moran @ 2003-09-23 19:50 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: gcc

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


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

* Re: Arrays and Alignment
  2003-09-23 19:40 ` Andreas Schwab
  2003-09-23 19:50   ` Michael N. Moran
@ 2003-09-23 19:56   ` Gabriel Dos Reis
  1 sibling, 0 replies; 11+ messages in thread
From: Gabriel Dos Reis @ 2003-09-23 19:56 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Michael N. Moran, gcc

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

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

* Re: Arrays and Alignment
  2003-09-23 19:50   ` Michael N. Moran
@ 2003-09-23 21:53     ` Neil Booth
  2003-09-23 22:10       ` Gabriel Dos Reis
  0 siblings, 1 reply; 11+ messages in thread
From: Neil Booth @ 2003-09-23 21:53 UTC (permalink / raw)
  To: Michael N. Moran; +Cc: Andreas Schwab, gcc

Michael N. Moran wrote:-

> >>% 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

He means, I think, that &a[n] == &a[0] + n * sizeof (a[0]) is an
inescapable fact of life.

Neil.

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

* Re: Arrays and Alignment
  2003-09-23 21:53     ` Neil Booth
@ 2003-09-23 22:10       ` Gabriel Dos Reis
  2003-09-23 22:17         ` Neil Booth
  0 siblings, 1 reply; 11+ messages in thread
From: Gabriel Dos Reis @ 2003-09-23 22:10 UTC (permalink / raw)
  To: Neil Booth; +Cc: Michael N. Moran, Andreas Schwab, gcc

Neil Booth <neil@daikokuya.co.uk> writes:

| Michael N. Moran wrote:-
| 
| > >>% 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
| 
| He means, I think, that &a[n] == &a[0] + n * sizeof (a[0]) is an
| inescapable fact of life.

Certainly, but alignment and sizeof are not unrelated attributes.
Since achar is not standard char, there is no requirement that
sizeof(achar)  be 1.  That should not be.  sizeof(achar) should be
determined by iyts alignment.

The following output
32
32
as I would expect

   #include <iostream>

   struct __attribute__((__aligned__(32))) A {
      char c;
   };

   int main()
   {
      A a[30]
      std::cout << __alignof(a[0]) << std::endl
                << sizeof(a[0]) << std::endl;
   }

There should be no difference with Michael's example.

-- Gaby

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

* Re: Arrays and Alignment
  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
  0 siblings, 2 replies; 11+ messages in thread
From: Neil Booth @ 2003-09-23 22:17 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: Michael N. Moran, Andreas Schwab, gcc

Gabriel Dos Reis wrote:-

> | > 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
> | 
> | He means, I think, that &a[n] == &a[0] + n * sizeof (a[0]) is an
> | inescapable fact of life.
> 
> Certainly, but alignment and sizeof are not unrelated attributes.

That may be true, but I stand by my statement, which I believe you
agree with, and I interpret that as what Andreas meant.

Neil.

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

* Re: Arrays and Alignment
  2003-09-23 22:17         ` Neil Booth
@ 2003-09-23 22:26           ` Gabriel Dos Reis
  2003-09-24  9:57           ` Andreas Schwab
  1 sibling, 0 replies; 11+ messages in thread
From: Gabriel Dos Reis @ 2003-09-23 22:26 UTC (permalink / raw)
  To: Neil Booth; +Cc: Michael N. Moran, Andreas Schwab, gcc

Neil Booth <neil@daikokuya.co.uk> writes:

| Gabriel Dos Reis wrote:-
| 
| > | > 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
| > | 
| > | He means, I think, that &a[n] == &a[0] + n * sizeof (a[0]) is an
| > | inescapable fact of life.
| > 
| > Certainly, but alignment and sizeof are not unrelated attributes.
| 
| That may be true, but I stand by my statement, which I believe you
| agree with,

More precisely, I do not agree with the statement that one can't
have both.  But I agree with the above relation.

| and I interpret that as what Andreas meant.


-- Gaby

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

* Re: Arrays and Alignment
  2003-09-23 22:17         ` Neil Booth
  2003-09-23 22:26           ` Gabriel Dos Reis
@ 2003-09-24  9:57           ` Andreas Schwab
  1 sibling, 0 replies; 11+ messages in thread
From: Andreas Schwab @ 2003-09-24  9:57 UTC (permalink / raw)
  To: Neil Booth; +Cc: Gabriel Dos Reis, Michael N. Moran, gcc

Neil Booth <neil@daikokuya.co.uk> writes:

> Gabriel Dos Reis wrote:-
>
>> | > 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
>> | 
>> | He means, I think, that &a[n] == &a[0] + n * sizeof (a[0]) is an
>> | inescapable fact of life.
>> 
>> Certainly, but alignment and sizeof are not unrelated attributes.
>
> That may be true, but I stand by my statement, which I believe you
> agree with, and I interpret that as what Andreas meant.

Yes, this is what I meant.  The only option we have is to increase sizeof,
as Gaby wrote.

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

^ 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, 0 replies; 11+ messages in thread
From: Gabriel Dos Reis @ 2003-09-23 21:05 UTC (permalink / raw)
  To: Richard Kenner; +Cc: gcc

kenner@vlsi1.ultra.nyu.edu (Richard Kenner) writes:

| | > typedef unsigned char achar __attribute__ ((__aligned__(32)));
| | > achar           a[2];
| 
| | > __alignof__(a[1]):1
| 
| I think this is wrong: it should be 32.

seconded.

|     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!

I stand corrected.

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

Thanks for the precision.

-- Gaby

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