[gnu.gcc] packed structures in GCC 1.36?

ji@close.cs.columbia.edu (John Ioannidis) (11/14/89)

Hi!

This topic has probably been discussed in the past, but I haven't been
reading gnu.gcc lately. So, suppose I have the following structure:

	struct _foo
	{
		char b1;
		short w1;
		short w2;
		char b2;
	};

On my machine (a 386 machine running 386/ix (System V R3.2)), the size
of _foo is eight bytes, with a "filler" byte between b1 and w1 and
another after b2. This is fine, and there are good reasons for doing
it. However, when I'm using that structure to access device registers
from a device that's memory-mapped (using a pointer that's of type
pointer-to-struct-_foo and setting it to the base of the mapped area),
I'm in trouble. 

My system compiler has an option (-Zp1) and the equivalent #pragma
directive (#pragma pack) that will force the structure fields to be
packed and get the size of the structure to be 6. But for reasons
beyond my control I cannot use it -- somebody else wrote the bulk of
the code I have to use in ANSI-C, complete with function prototypes
and pointers to void! I'd rather not hack a couple of thousand lines of code
if there is a way to do it with gcc. And yes, I know about unprotoize, 
but it won't do the trick!

Thanks in advance,

/ji

In-Real-Life: John "Heldenprogrammer" Ioannidis
E-Mail-To: ji@cs.columbia.edu
V-Mail-To: +1 212 854 5510
P-Mail-To: 450 Computer Science \n Columbia University \n New York, NY 10027

rfg@ics.uci.edu (Ron Guilmette) (11/22/89)

In article <6632@columbia.edu> ji@close.cs.columbia.edu (John Ioannidis) writes:
>Hi!
>
>This topic has probably been discussed in the past, but I haven't been
>reading gnu.gcc lately. So, suppose I have the following structure:
>
>	struct _foo
>	{
>		char b1;
>		short w1;
>		short w2;
>		char b2;
>	};
>
>On my machine (a 386 machine running 386/ix (System V R3.2)), the size
>of _foo is eight bytes, with a "filler" byte between b1 and w1 and
>another after b2. This is fine, and there are good reasons for doing
>it. However, when I'm using that structure to access device registers...

Try this:

	struct _foo {
		unsigned b1:8;
		unsigned w1:16;
		unsigned w2:16;
		unsigned b2:8;
	};

Note that ANSI requires the fields to be unsigned, but GCC probably doesn't
care.

// rfg