structure packing

Mohamed Shafi shafitvm@gmail.com
Thu May 21 14:39:00 GMT 2009


Hello all,

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 am assuming that this happens because when 'packed' attribute is
provided the compiler assumes that there is no alignment to natural
boundary and so produces code for byte access.  Am i right?

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?

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.

Regards,
Shafi



More information about the Gcc-help mailing list