[lug] OFF: sizeof structs in gcc?

Sebastian spsobole at mindless.com
Thu Jun 7 17:43:06 MDT 2001


On Thursday 07 June 2001 01:11 pm, you wrote:
> "Scott A. Herod" wrote:
> > Hello,
> >
> > This is a kind of off topic but I'm hoping to avoid the gcc lists.
> >
> > Is it true that gcc allocates space for objects/stucts in 4 byte
> > words?  Is there a way to avoid this?
> >
> > Looks like winder's VC has a "#pragma pack" that can compact objects
> > but gcc seems to have nothing comparable.  It's not really a problem.
> > That's what bcopy is for.  I'm mainly just curious.
> >
> > Thanks,
> >
> > Scott Herod
>
> I'd make some guesses on this, but definitely don't consider it a fact.
> One guess is that that the x86 address pointer is 32 bit, and it is 4
> bytes in order to point at the address of start. The other guess might
> be that it is done to keep structs aligned on 32 bit
> boundaries...packing could have some really bad performance penalties if
> not (at least on x86), such as randomly packing the start of a 7 byte
> struct right after a 5 byte struct. Out of curiosity, are you trying to
> avoid a lot of memory use where you can't afford to waste memory?
>
> D. Stimits, stimits at idcomm.com
> _______________________________________________

Yes. GCC by default alligns structures to the native boundry for the cpu the 
code is compiled.  32 bits on most 32 bit platforms like x86 ( 386+).  You 
can alter the default alignment using #pragma directives in your source.  
This is done for speed.  On a 32 bit machine a "mov" instruction can move 32 
bits of data as fast as 16bits of data. ( ie: in a single instruction )  Of 
course this is done at the cost of memory usage.

For a good reference look at : http://gcc.gnu.org

Specifically:  http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_17.html#SEC202

-Sebastian



More information about the LUG mailing list