public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* Initialising an union.
@ 1998-08-17 7:51 Aurel Balmosan
1998-08-18 11:46 ` Jeroen Dobbelaere
0 siblings, 1 reply; 10+ messages in thread
From: Aurel Balmosan @ 1998-08-17 7:51 UTC (permalink / raw)
To: egcs-bugs
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1234 bytes --]
Hello,
I noticed a problem while initialising an union.
Here a test source:
struct a {
       int a;
};
struct b {
       int a;
       double b;
};
struct c {
       int a;
       double b;
       char c[16];
};
union x {
       int a;
       struct a as;
       struct b bs;
       struct c cs;
};
void test()
{Â Â Â Â Â Â union x X = { 0 };
       for(;;);
       X.a++;
}
void main()
{      char   c[]="1123124213";
       test();
}
You will notice that the union X is not fully initialised. At least
I noticed that
on DecUNIX-4.0b (4.0d) Solaris-2.5.1 with the egcs version
 egcs-2.90.29 980515 (egcs-1.0.3 release)
Â
--Â
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
Â
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union.
1998-08-17 7:51 Initialising an union Aurel Balmosan
@ 1998-08-18 11:46 ` Jeroen Dobbelaere
1998-08-19 4:40 ` Aurel Balmosan
0 siblings, 1 reply; 10+ messages in thread
From: Jeroen Dobbelaere @ 1998-08-18 11:46 UTC (permalink / raw)
To: Aurel Balmosan; +Cc: egcs-bugs
Hi,
The initialisation of a union only initializes the first member
of the union, not the 'largest' member. (the remaining part can be
uninitialized.
(from the december 1997 draft working paper :)
8.5 Initializers [dcl.init]
1 A declarator can specify an initial value for the identifier being
[...]
5 To zero-initialize storage for an object of type T means:
[..]
--if T is a union type, the storage for its first data member5) is
zero-initialized;
[.....]
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. ]
Greetings,
Jeroen Dobbelaere
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union.
1998-08-18 11:46 ` Jeroen Dobbelaere
@ 1998-08-19 4:40 ` Aurel Balmosan
1998-08-19 10:03 ` Jeroen Dobbelaere
0 siblings, 1 reply; 10+ messages in thread
From: Aurel Balmosan @ 1998-08-19 4:40 UTC (permalink / raw)
To: Jeroen Dobbelaere, egcs-bugs
Jeroen Dobbelaere wrote:
> 5 To zero-initialize storage for an object of type T means:
> [..]
>
> --if T is a union type, the storage for its first data member5) is
> zero-initialized;
>
> [.....]
>
> 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:
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union.
1998-08-19 4:40 ` Aurel Balmosan
@ 1998-08-19 10:03 ` Jeroen Dobbelaere
0 siblings, 0 replies; 10+ messages in thread
From: Jeroen Dobbelaere @ 1998-08-19 10:03 UTC (permalink / raw)
To: Aurel Balmosan; +Cc: egcs-bugs
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union
1998-08-20 2:41 ` Aurel Balmosan
@ 1998-08-20 8:15 ` Alexandre Oliva
0 siblings, 0 replies; 10+ messages in thread
From: Alexandre Oliva @ 1998-08-20 8:15 UTC (permalink / raw)
To: Aurel Balmosan; +Cc: egcs-bugs
Aurel Balmosan <ab@orga.com> 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 :-(
[snip]
> 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 };
assert(c[sizeof(int)]==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
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union
1998-08-19 14:51 ` Alexandre Oliva
@ 1998-08-20 2:41 ` Aurel Balmosan
1998-08-20 8:15 ` Alexandre Oliva
0 siblings, 1 reply; 10+ messages in thread
From: Aurel Balmosan @ 1998-08-20 2:41 UTC (permalink / raw)
To: Alexandre Oliva, egcs-bugs
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 :-(
It does. In the ANSI-C Language Referenze Version 2 A.8.7
from 31.Oct.1988 ANSI Programming Langues C, X.3.159-1989
you find the sentence:
In the first edition the initialisation of auto and register
struct's, union's or vektors was not allowed. The ANSI-C
standard allowes to use const constructors or simple
initilizer expresions.
This make clear that auto and register structs/unions/vektors
can be initialised.
Also I used the same code on different platforms i.e DEC-Unix
where we use the standard C compiler which comes with the OS.
There the code works as I expected.
So now I am back that the egcs-1.0.3a has a bug here.
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union
1998-08-19 2:44 ` Aurel Balmosan
@ 1998-08-19 14:51 ` Alexandre Oliva
1998-08-20 2:41 ` Aurel Balmosan
0 siblings, 1 reply; 10+ messages in thread
From: Alexandre Oliva @ 1998-08-19 14:51 UTC (permalink / raw)
To: Aurel Balmosan; +Cc: egcs-bugs
Aurel Balmosan <ab@orga.com> writes:
> As it is defined in ANSI-C a struct where only the first part is initialised
> is filled with zero's.
> This is defined in the ANSI-C Language Reference A.8.7:
> Contains the initializer-list less initializer then the structure component
> has the rest values will be set to zero. There must not be more initializer
> than values in a structure component.
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 :-(
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union
1998-08-18 18:14 ` Alexandre Oliva
@ 1998-08-19 2:44 ` Aurel Balmosan
1998-08-19 14:51 ` Alexandre Oliva
0 siblings, 1 reply; 10+ messages in thread
From: Aurel Balmosan @ 1998-08-19 2:44 UTC (permalink / raw)
To: Alexandre Oliva, egcs-bugs
Alexandre Oliva wrote:
> Aurel Balmosan <ab@orga.com> writes:
>
> > You will notice that the union X is not fully initialised.
>
> What do you mean by fully initializing a union? You have initialized
> it with only an integer, so only the first integer field will be
> initialized; the remaining fields need not be, because you have not
> initialized them.
As it is defined in ANSI-C a struct where only the first part is initialised
is filled with zero's. As for gcc-2.7.2.3 and older versions the union was
fully initialised. This is not any more with egcs-1.0.3a which we now use.
Either the gcc-2.7.2.3 and older used a not ANSI-C comform initialising
for unions or the egcs-1.0.3a does not follow ANSI-C in this part.
structs are fully initialised to zero when one say ie:
struct c {
int a;
double b;
char c[123];
};
struct d {
char c[234];
char d[231];
int e;
};
struct c Y = { 0 };
struct d Z = { 0 };
This is with gcc-2.7.2.3 and older as well as egcs-1.0.3a. Only the union
is not! And thats my problem and a bug in egcs-1.0.3a
This is defined in the ANSI-C Language Reference A.8.7: Initialisation
I have currently only the german version but here the sentence which
describes that behaviour and applies for struct's as well as for union's:
initializer:
assignment-expression
{ initializer-list }
{ initializer-list, }
initializer-list:
initializer,
initializer-list, initializer
Contains the initializer-list less initializer then the structure component
has the rest values will be set to zero. There must not be more initializer
than values in a structure component.
I think that makes it clear.
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Initialising an union
1998-08-18 0:39 Aurel Balmosan
@ 1998-08-18 18:14 ` Alexandre Oliva
1998-08-19 2:44 ` Aurel Balmosan
0 siblings, 1 reply; 10+ messages in thread
From: Alexandre Oliva @ 1998-08-18 18:14 UTC (permalink / raw)
To: Aurel Balmosan; +Cc: egcs-bugs
Aurel Balmosan <ab@orga.com> writes:
> You will notice that the union X is not fully initialised.
What do you mean by fully initializing a union? You have initialized
it with only an integer, so only the first integer field will be
initialized; the remaining fields need not be, because you have not
initialized them.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
^ permalink raw reply [flat|nested] 10+ messages in thread
* Initialising an union
@ 1998-08-18 0:39 Aurel Balmosan
1998-08-18 18:14 ` Alexandre Oliva
0 siblings, 1 reply; 10+ messages in thread
From: Aurel Balmosan @ 1998-08-18 0:39 UTC (permalink / raw)
To: egcs-bugs
Hello,
I noticed a problem while initialising an union.
Here a test source:
struct a {
int a;
};
struct b {
int a;
double b;
};
struct c {
int a;
double b;
char c[16];
};
union x {
int a;
struct a as;
struct b bs;
struct c cs;
};
void test()
{ union x X = { 0 };
for(;;);
X.a++;
}
void main()
{ char c[]="1123124213";
test();
}
You will notice that the union X is not fully initialised. At least I noticed
that
on DecUNIX-4.0b (4.0d) Solaris-2.5.1 with the egcs version
egcs-2.90.29 980515 (egcs-1.0.3 release)
--
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
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~1998-08-20 8:15 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-08-17 7:51 Initialising an union Aurel Balmosan
1998-08-18 11:46 ` Jeroen Dobbelaere
1998-08-19 4:40 ` Aurel Balmosan
1998-08-19 10:03 ` Jeroen Dobbelaere
1998-08-18 0:39 Aurel Balmosan
1998-08-18 18:14 ` Alexandre Oliva
1998-08-19 2:44 ` Aurel Balmosan
1998-08-19 14:51 ` Alexandre Oliva
1998-08-20 2:41 ` Aurel Balmosan
1998-08-20 8:15 ` Alexandre Oliva
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).