[LCP]struct size

Andrew Weaver Andrew.Weaver at tecnomen.fi
Mon Aug 20 18:10:48 UTC 2001


Yes, see __attribute__((packed)) 

> -----Original Message-----
> From:	Robert Wuest [SMTP:rwuest at wuest.org]
> Sent:	Sunday, August 19, 2001 3:34 AM
> To:	linuxcprogramming at lists.linux.org.au
> Subject:	Re: [LCP]struct size
> 
> I've run into this before. I had a very large struct; handling each item
> individually was a real pain. Is there any way, with gcc, to force this
> to pack (ie., somestruct be 6 bytes)?  Some attribute when declaring the
> struct or a compiler option to prevent word alignment?
> 
> Robert
> 
> Roberto Diaz wrote:
> > 
> > > please see this code
> > > struct somestruct
> > > {
> > >  int a;
> > >  char b;
> > >  char c;
> > > }stvar;
> > > int main()
> > > {
> > >   printf("%d",sizeof stvar); /* you dont need () since it is an
> instance*/
> > > }
> > > the o/p is 8.  i was expecting 6 as 'a' occupies 4 bytest and 'b','c'
> > > one byte each !  how is it 8 ??
> > 
> > Aligment.. the word size in i386 is 32 bits 4 bytes so the compiler left
> > holes into your structure so all is aligned at 4 bytes boundaries.
> > 
> > Regards
> > 
> > Roberto
> > 
> > ------------------------------------------------------------------------
> > Roberto Diaz <rdiazmartin at vivaldi.dhis.org>
> > http://vivaldi.dhis.org
> > Powered by GNU running on a Linux kernel.
> > Powered by Debian (The real wonder)
> > 
> > Concerto Grosso Op. 3/8 A minor
> > Antonio Vivaldi (so... do you need beautiful words?)
> > ------------------------------------------------------------------------
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List



More information about the linuxCprogramming mailing list