struct packing reliability?

Matthew Bloch mattbee@soup-kitchen.net
Wed Feb 27 04:18:00 GMT 2002


On Wednesday 27 February 2002 4:19 am, Andrew Lees wrote:
> Hi,
>
> I'm not sure this will help, but have you tried
>
> #pragma pack(1)

Yes, same results as when I use the __attribute__ qualifier.

Unless there's something I'm missing, I think it's impossible to pack 
structs-within-structs on bit boundaries, as this test case demonstrates:

------------------------------------------------------------------------
#pragma pack(1)

struct test1
{
   int a : 5;
   struct {
      int b1 : 3;
      int b2 : 2;
   } b;
   int c : 5;
   int d : 1;
};

struct test2
{
   int a : 5;
   int b : 5;
   int c : 5;
   int d : 1;
};

int main(void)
{
   printf("%d %d\n", sizeof(struct test1), sizeof(struct test2));
   /* prints '3 2', annoyingly */
}
 
#pragma pack()
------------------------------------------------------------------------

The non-portability aspect isn't a major concern for me, given that this is 
for a fixed hardware platform and fixed compiler, but it's a shame that I 
can't get the behaviour I'm after because it makes use of these structures 
that little bit uglier.

Unless anybody knows better...?

-- 
Matthew       > http://www.soup-kitchen.net/
              > ICQ 19482073



More information about the Gcc-help mailing list