[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Programming question: sizeof struct?



On Fri, 9 Jul 1999, Oleg Krivosheev wrote:

> > to not be handled properly.  Even though I specify -fpack_struct the
> > generated code does not appear to actually do this.  Structure fields
> > are
> > offset and the return from sizeof() returns a value that is not valid.
> 
> what do you mean it is not valid? Read ANSI standard or any
> decent C book. It is quite valid - compiler may insert any padding
> between struct members...

He probably means it is not exactly what he wants :>

> > For instance, if the structure were:
> > 
> > struct foo {
> >     char text[3];
> >     int  num;
> > };
> > 
> > sizeof would return 6 and not 5.  
> 
> 6? Are you sure you're using Linux/gcc?

Yes, 6, it will insert a single extra character at the end of text to
place the alignment of num on a 4 byte boundry.

> > So it's obvious that the compiler is
> > placing a pad byte between text and num to align num.  I want it to
> > stop!

I always hated gcc __attribute__, I prefer the simpler and more common

#pragma pack(1)
struct {};
#pragma pack()

Which forces the layout to be as you specified.

Using a command line option is a Bad Idea (tm) as it may corrupt glibc's
structures

Jason


Reply to: