Re: Programming question: sizeof struct?
Hi,
On Fri, 9 Jul 1999, Jason Gunthorpe wrote:
> 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.
Once again, sizeof(int) = 4 on Linux/ia32 and Linux/SPARC
(that is what i can check),
sizeof(text) is at least 3, therefore sizeof( struct foo )
should be at least 7 !!! With additional padding byte sizeof would be
equal to 8.
What platform/OS/compiler are you using?
>
> > > 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.
i got an impression it doesn't work for the code...
if i misinterpreted the original message, i'm sorry
> Using a command line option is a Bad Idea (tm) as it may corrupt glibc's
> structures
Attributes have nothing in common with command line switches, they're
just like pragmas
>From gcc info:
The keyword `__attribute__' allows you to specify special attributes
of variables or structure fields.
> Jason
Anyway, i think better redesign and rewrite struct I/O stuff.
pragma pack and __attribute__ is just gross hack
OK
Reply to: