The following function should set s1.m3 to 0 since there is not an
initializer for it.
>From K&R 2e, p. 219, middle of the page:
"The initializer for a structure is either an expression of the same
type, or a brace-enclosed list of initializers for its members in
order. Unnamed bif-field members are ignored, and are not
initialized. If there are fewer initializers in the list than members
of the structure, the trailing members are initialized with 0. ..."
void
finit (void)
{
struct s1
{
unsigned m1:2;
unsigned :2;
unsigned m2:2;
unsigned m3:2;
} s1 = { 1, 2 };
if (s1.m3 != 0)
printf("ERROR: s1.m3 = %d\n", s1.m3);
}
But looking at the whirl comming out of gccfe, we see that only the m1 and
m2 fields are initialized. m3 has an unitialized value (equal to whatever
garbage is on the stack at that location).
LOC 0 0 source files: 1 ""
LOC 1 2 main ()
LOC 1 3 {
FUNC_ENTRY <1,20,main>
BODY
BLOCK
END_BLOCK
BLOCK
END_BLOCK
BLOCK
PRAGMA 0 120 <null-st> 0 (0x0) # PREAMBLE_END
LOC 1 6 unsigned m1:2;
LOC 1 7 unsigned :2;
LOC 1 8 unsigned m2:2;
LOC 1 9 unsigned m3:2;
LOC 1 10 } s1 = { 1, 2 };
U4INTCONST 1 (0x1)
BSSTID 0 <2,1,s1> T<28,s1,4> <field_id:1>
U4INTCONST 2 (0x2)
BSSTID 0 <2,1,s1> T<28,s1,4> <field_id:3>
U4INTCONST 0 (0x0)
U8LDA 0 <2,1,s1> T<29,anon_ptr.,8>
U4INTCONST 3 (0x3)
MSTORE 1 T<30,anon_ptr.,8>
LOC 1 11
LOC 1 12 if (s1.m3 != 0)
IF
U4BSLDID 0 <2,1,s1> T<28,s1,4> <field_id:4>
I4U4CVT
I4INTCONST 0 (0x0)
I4I4NE
THEN
BLOCK
LOC 1 13 printf("ERROR: s1.m3 = %d\n", s1.m3);
U8LDA 0 <1,22,(19_bytes)_"ERROR:_s1.m3_=_%d\n\000"> T<34,anon_ptr.,8>
U8PARM 2 T<31,anon_ptr.,8> # by_value
U4BSLDID 0 <2,1,s1> T<28,s1,4> <field_id:4>
I4U4CVT
I4PARM 2 T<4,.predef_I4,4> # by_value
VCALL 126 <1,21,printf> # flags 0x7e
END_BLOCK
ELSE
LOC 1 12
BLOCK
END_BLOCK
END_IF
RETURN
END_BLOCK
|