From mboxrd@z Thu Jan 1 00:00:00 1970 From: Jeroen Dobbelaere To: Aurel Balmosan Cc: egcs-bugs Subject: Re: Initialising an union. Date: Wed, 19 Aug 1998 10:03:00 -0000 Message-id: <35DB0717.430E5814@tvd.be> References: <35D842BF.93D35CB8@orga.com> <35D9C176.873FB5F6@tvd.be> <35DAB938.618E3FFE@orga.com> X-SW-Source: 1998-08/msg00386.html List-Id: Yes, I forgot to say that this is only true for C++ (See http://www.cs.sbcc.net/~shouk/cppdraft/cd2/ for a copy of the working paper) (maybe something to add to the faq, although it is not the definitive standard :( ) I don't know what it should be for C++. Also note that in your example, you are using an **auto-variable** : void test() { union x X = { 0 }; for(;;); X.a++; } It also seems that I mixed up static/global variables and auto : This is the relevant section for this problem : 8.5.1 Aggregates [dcl.init.aggr] [..] 15When a union is initialized with a brace-enclosed initializer, the braces shall only contain an initializer for the first member of the union. [Example: union u { int a; char* b; }; u a = { 1 }; u b = a; u c = 1; // error u d = { 0, "asdf" }; // error u e = { "asdf" }; // error --end example] [Note: as described above, the braces around the ini- tializer for a union member can be omitted if the union is a member of another aggregate. ] It seems not to say anything about the initialization of remaining bytes. Because those are on the stack, those (probably) wont be initialized -> so you should explicitly initialize the member you need. Aurel Balmosan wrote: > [..] > > Well in the ANSI-C books I have this is not defined in this way. It is only > said that unions can now be initialised (ANSI-C version 2) > > So it seems to not a bug in egcs-1.0.3a but what draft is it? C++ or ANSI-C? > > Bye, > > Aurel. > > -- > Aurel Balmosan | Department SE-SW > ORGA Kartensysteme GmbH | phone: +49 5254 991 824 > An der Kapelle 2 | fax : +49 5254 991 749 > 33104 Paderborn, Germany | mailto:ab@orga.com