This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Arrays and Alignment
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