[LCP]struct size

Robert Wuest rwuest at wuest.org
Sun Aug 19 10:46:54 UTC 2001


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?)
> ------------------------------------------------------------------------



More information about the linuxCprogramming mailing list