Initialising an union

Alexandre Oliva
Thu Aug 20 08:15:00 GMT 1998

Aurel Balmosan <> writes:

> Alexandre Oliva wrote:
>> Does it apply to local non-static variables?  I'm pretty sure this is
>> true for variables in static storage, but how about local ones?
>> Unfortunately, I don't have access to the ANSI-C Standard :-(


> This make clear that auto and register structs/unions/vektors
> can be initialised.

That was not my question.  I asked whether the standard explicitly
stated that uninitialized members of an automatic union would be
implicitly zero-initialized.  A summary of the related posts to the
list follows:

1) automatic unions can be explicitly initialized

2) if a initializer-list contains less elements than the initialized
object, the remaining elements are zero-initialized

3) when a union is explicitly initialized, the initializer-list will
be used to initialize the first element of the union

so, given the following code snippet:

union t {
  int i;
  char c[sizeof(int)+1];

void foo() {
  union t x = { 0 };

Given only the three statements listed above, the assertion *can*
fail, because the second type of the union (c) is *not* initialized,
only the first one (i) is.  Thus, it needs not be zero initialized,
based on the claims above.

As I have stated earlier, I don't have a copy of the C Standard, and
I'd be happy to find out that the Standard does specify that union
initialization must be performed as if the whole union were
zero-initialized before explicit initialization takes place.  It's
just that I haven't read (or understood :-) any claim that supports
this interpretation, and it doesn't look much like C to me :-)

Alexandre Oliva
Universidade Estadual de Campinas, SP, Brasil

More information about the Gcc-bugs mailing list