RE: [ILUG] gcc optimisation weirdness?

From: Brendan Kehoe (brendan at domain zen.org)
Date: Thu 11 May 2000 - 10:34:14 IST


"Caolan McNamara" <cmc at domain stardivision.de> wrote:
> i.e.  int apple:4; creates a var apple which is 4bits long.
> You would not see :NULL in reality and god only knows what :0 would do 

The empty bit field like
struct foo {
   char x;
   int :0;
   char z;
};
would be used to force the alignment of Z to be on a proper 4-byte
(sizeof int) boundary.  Thus, it'd map to a memory layout of:
   0: x
   1:
   2:
   3:
   4: z

If you'd done it with a char like

struct foo {
  char x;
  char :0;
  char z;
};

it would only call for alignment on a 1-byte (sizeof char) boundary.
That becomes

  0: x
  1: y

According to C9X $6.7.2.1,  "If the value is zero, the declaration shall
have no declarator" deems it incorrect to give that zero-width bitfield
a name.  In paragraph 11 of that section, it says, "As a special case, a
bit-field structure member with a width of 0 indicates that no further
bit-field is to be packed into the unit in which the previous bit-field,
if any, was placed."

It goes on in paragraph 12 to say that "Each non-bit-field member of a
structure or union object is aligned in an implementation-defined manner
appropriate to its type."

Soooooo, I interpret all of that to mean that it's up to the compiler to
figure out what it wants to do with a zero-width unnamed bitfield,
beyond controlling the alignment of other bitfields.  Other structure
data members that aren't bitfields are free to do as they like; gcc
takes the route of realigning them much as it would the bitfields
themselves.  Other compilers may ignore this unnamed element and just
promote everything to be aligned on 4-byte boundaries.

Glad you asked?

:-)

B



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:06:06 GMT