PDA

View Full Version : padding non 4 byte data to 4 bytes: getting rid of it


grady
10-11-2002, 05:36 PM
if you have a struct


struct bloat
{
short x;
long y;
short z;
};
sizeof( bloat ) = 12 because the compiler pads the 2 byte shorts to 4 bytes however


struct bloat
{
long x;
short y;
short z;
};


doesn't need the padding and will be 8 bytes long.

I think in MSVC their is a pragma directive that can force the compiler to not do this. Is there a similar thing I could use to get gcc/g++ to not pad the 2 byte values.

bwkaz
10-11-2002, 07:36 PM
http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Variable-Attributes.html#Variable%20Attributes

The online GCC 3.2 reference manual, or at least one page of it, found at gcc.gnu.org. Scroll down to the "packed" section.

Also, see the bottom of this (http://gcc.gnu.org/onlinedocs/gcc-3.2/gcc/Function-Attributes.html#Function%20Attributes) page, for why the GCC team thinks that using #pragma for anything is a really bad idea. It has to do with cross-platform-ability.