RE: Align attribute on struct

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Ivan,

Yes, int is 4 bytes but the struct should be aligned to 16 bytes (a, b, c,
and 4 bytes of padding).

Actually, I found the problem was with the syntax as specified in gcc's
online docs. The correct syntax should be:

typedef struct __attribute__((__aligned__(16))) {
    int a, b, c;
} s;

Making this change makes it work.

Thank you,

Rodrigo

> -----Original Message-----
> From: ivanko.rus [mailto:ivanko.rus@xxxxxxxxx]
> Sent: Wednesday, July 07, 2010 6:41 PM
> To: roddomi@xxxxxxxxxxx; gcc-help@xxxxxxxxxxx
> Subject: Re: Align attribute on struct
> 
> 2010/7/7 Rodrigo Dominguez <roddomi@xxxxxxxxxxx>
> >
> > I am trying to ask gcc to align a struct but it is not working.
> Here's a
> > program I wrote to show what I am trying to do:
> >
> > #include <stdio.h>
> >
> > typedef struct {
> >    int a, b, c;
> > } s __attribute__ ((aligned (16)));
> >
> > int data[] = {1, 2, 3, 4, 5, 6, 7, 8};
> >
> > int main(void)
> > {
> >    printf("%d\n", ((s *)data)[0].a);
> >    printf("%d\n", ((s *)data)[1].a);
> >
> >    return 0;
> > }
> >
> > Compiling with gcc 4.4.1:
> >
> > $ gcc test.c
> > $ ./a.out
> > 1
> > 4
> >
> > I would expect the output to be 1 and 5. Am I missing something?
> >
> > Thank you,
> >
> > Rodrigo
> >
> Hello, Rodrigo! Sorry if I'm wrong, but, as I remember, int is 4 bytes
> long, 32 bits, so the structure in your case will be "continuous". So
> everything is working as it should! Regards, Ivan.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux