Structure alignment problem

Daniel Lidsten Daniel.Lidsten@combitechsystems.com
Tue Sep 17 02:53:00 GMT 2002


Hi,

I recall that you actually have to use the attribute command to *all*
variables inside the structure. This is because you want to pack the
variables to be located side-by-side. When you give the whole structure
the attribute then you tell the compiler to put the next variable or
structure packed to the meantioned structure, not to actually pack the
ingoing variables.

So, you need to write it like this:

struct {
	int a 	__attribute__ ((packed));
	char b 	__attribute__ ((packed));
	int c 	__attribute__ ((packed));
} pippo;

For a more detailed description about this attribute see the following
manual page:

http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC90

Regards, Daniel

> -----Original Message-----
> From: Yves Rutschle [mailto:y.rutschle@indigovision.com] 
> Sent: den 16 september 2002 13:22
> To: Rosetti Andrea
> Cc: Red Hat cossgcc mailing
> Subject: Re: Structure alignment problem
> 
> 
> Hello,
> 
> On Thu, Sep 12, 2002 at 11:48:45AM +0200, Rosetti Andrea wrote:
> > My problem is to set the max alignment size for the 
> structure members 
> > because I use different compilers for different targets and I have 
> > compatibility problems.
> > 
> > For example:
> > 
> > struct {
> >  int a;
> >  char b;
> >  int c;
> > } pippo;
> > 
> > void bye ()
> > {
> >   pippo.a=4;
> >   pippo.b='4';
> >   pippo.c=9;
> > 
> > }
> 
> You can't expect C to produce consistent code for something 
> like that. Alignement isn't defined. All you know is that the 
> same compiler will use a consistent policy (ie if you use 
> that same struct in 2 different places, you'll get the same 
> alignement).
> 
> If you need to exchange structs between targets (or even 
> between applications produced with different compilers, I'd 
> guess), you'll need to define your structs as packed in a way 
> I can't quite recall but it'd look something like:
> 
> struct {
>   int a;
>   char b;
>   int c;
> } pippo __attribute("PACK");
> 
> See GCC Manual for details...
> 
> HTH
> /Y
> 
> 
> ------
> Want more information?  See the CrossGCC FAQ, 
> http://www.objsw.com/CrossGCC/ Want to > unsubscribe? Send a 
> note to crossgcc-unsubscribe@sources.redhat.com
> 
> 

------
Want more information?  See the CrossGCC FAQ, http://www.objsw.com/CrossGCC/
Want to unsubscribe? Send a note to crossgcc-unsubscribe@sources.redhat.com



More information about the crossgcc mailing list