This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: structure packing
- From: Ian Lance Taylor <iant at google dot com>
- To: Mohamed Shafi <shafitvm at gmail dot com>
- Cc: gcc-help at gcc dot gnu dot org, Rohit Arul Raj <rohitarulraj at gmail dot com>
- Date: Thu, 21 May 2009 23:33:45 -0700
- Subject: Re: structure packing
- References: <ba0bd44d0905210723n5e6e85f5rea8b2b52c381d5d9@mail.gmail.com> <ba0bd44d0905210738x38bd4022s9a7b86cbce1ecadd@mail.gmail.com>
Mohamed Shafi <shafitvm@gmail.com> writes:
> The below code is compiled using a private port based on GCC 4.1.1.
>
> struct data {
> ÂÂÂÂ int wdata;
> ÂÂÂÂ int rdata;
> Â};// __attribute__((__packed__));
>
> typedef struct data data;
> #define data_p (*(volatile data *)(0))
>
> int main(void)
> {
> Â data_p.wdata = data_p.rdata;
> }
>
> For the above source code without the 'packed' attribute the assembly
> code is generated with word-aligned access. But with packed attribute
> enabled the assembly code is generated with byte access. When i looked
> into x86 compiler i find that the assembly code generated is same
> irrespective of whether the attribute is provided or not.
I don't see this. When I compile the above test case (i.e., the struct
is not packed) on x86, I see word accesses.
> In case,
> 1) if the packing is enabled and all the data elements inside the
> struct are by default word-aligned (all the 32bit integers) is it
> possible to generate word-aligned assembly code?
No. The packed attribute tells the compiler that the struct may be
misaligned.
> 2) Packing is enabled for a structure with different data elements.
>
> i.e.
> struct data {
> ÂÂÂÂ int wdata;
> ÂÂÂ char t;
> ÂÂÂÂ int rdata;
> Â} __attribute__((__packed__));
>
> Is it possible to generate code like:
> wdata -> word aligned access.
> rdata -> byte aligned access.
No. You could try putting a packed struct inside the unpacked struct,
though; I don't know whether or not that would work.
Ian