[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

UNION global variabl initialize

Lung.S.wu

9/12/2011 4:39:00 PM

From ANSI C definition, section 6.7.8, with UNION, it says that only the first named element will be initialized if it is not initialized explicitly.
That means, such the code
1 typedef union {
2 char a;
3 short b;
4 int c;
5 } UU;
6
7 UU u;
8
9 int main(void)
10 {
11
12 return 0;
13 }
Only the member u.a is initialized as 0, And others are undefined.

But from modern system design (ELF), all memory of the global variable u will be cleared to 0. Because u is saved at BSS, and program loader will clear BSS segment after a executable file is load into main memory.

My question...
1. Does exist any system that will not clear remain elements of global union variable?
2. Why ANSI C org. will not define all union members be cleared to zero?
14 Answers

jt

9/12/2011 5:08:00 PM

0

Lung.S.Wu <lung.s.wu@gmail.com> wrote:
> From ANSI C definition, section 6.7.8, with UNION, it says that only
> the first named element will be initialized if it is not initialized
> explicitly.
> That means, such the code
> 1 typedef union {
> 2 char a;
> 3 short b;
> 4 int c;
> 5 } UU;
> 6
> 7 UU u;
> 8
> 9 int main(void)
> 10 {
> 11
> 12 return 0;
> 13 }
> Only the member u.a is initialized as 0, And others are undefined.

> But from modern system design (ELF), all memory of the global variable
> u will be cleared to 0. Because u is saved at BSS, and program loader
> will clear BSS segment after a executable file is load into main memory.

That may be the case for a number of systems, but the C standard
isn't made for a sub-class of the exising systems, it tries to
address, as far as possible, all kinds of systems.

> My question...
> 1. Does exist any system that will not clear remain elements of global
> union variable?
> 2. Why ANSI C org. will not define all union members be cleared to zero?

There are definitely systems where e.g. a floating point value
or a NULL pointer has a representation that's not all-bits-zero.
If on such a system you have a union that contains one of those
as well as a member that has an all-bit-zero representation it
would be impossible to initialize both members at the same time
to a "zero" value. Thus it would be counterproductive if the C
standard would request that more than a single element is initi-
alized to a "zero" value since it would make it impossible to
write a conforming compiler for such a system

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

Kenneth Brody

9/12/2011 7:03:00 PM

0

On 9/12/2011 12:38 PM, Lung.S.Wu wrote:
> From ANSI C definition, section 6.7.8, with UNION, it says that only the first named element will be initialized if it is not initialized explicitly.
> That means, such the code
> 1 typedef union {
> 2 char a;
> 3 short b;
> 4 int c;
> 5 } UU;
> 6
> 7 UU u;
> 8
> 9 int main(void)
> 10 {
> 11
> 12 return 0;
> 13 }
> Only the member u.a is initialized as 0, And others are undefined.
>
> But from modern system design (ELF), all memory of the global variable u will be cleared to 0. Because u is saved at BSS, and program loader will clear BSS segment after a executable file is load into main memory.
>
> My question...
> 1. Does exist any system that will not clear remain elements of global union variable?
> 2. Why ANSI C org. will not define all union members be cleared to zero?

Well, as answered elsethread, it may not be possible to initialize all
elements to zero. (ie: a system where NULL is not all-bits-zero.)

Consider, too, what happens if the first named element is a pointer on such
a system. You can't use BSS on such a system (assuming that BSS even exists
on such a system) because it's not initialized to all-bits-zero.

union {
char *pt;
int (*func)(void);
float f;
double d;
}
foo;

--
Kenneth Brody

Lung.S.wu

9/13/2011 5:10:00 AM

0

Yes, agree with you.
Is there any system which defines NULL, floating point 0 not all bits-zero?

Ike Naar

9/13/2011 7:10:00 AM

0

On 2011-09-13, Lung.S.Wu <lung.s.wu@gmail.com> wrote:
> Is there any system which defines NULL, floating point 0 not all bits-zero?

This is a FAQ.
http://c-faq.com/null/mach...

jt

9/13/2011 11:42:00 AM

0

Ike Naar <ike@iceland.freeshell.org> wrote:
> On 2011-09-13, Lung.S.Wu <lung.s.wu@gmail.com> wrote:
> > Is there any system which defines NULL, floating point 0 not all bits-zero?

> This is a FAQ.
> http://c-faq.com/null/mach...

And I remember to have read that on 286 and 386 machines without
a math co-processor floating point operations were done via soft-
ware emulation using a non-IEEE 754 format where 0.0 had a non-
all-bits-zero representation (but I may mis-remember, never ha-
ving used that kind of machines;-). And, while I have no expe-
rience with embedded systems, I wouldn't be too surprised if
one would find something similar on those. Furthermore, there
is nothing that keeps people developing new architectures that
don't use IEEE 754 or have non-zero NULL pointers...

So it's not really so much the question if such machines exit
but that, since the C standard doesn't require an all-bits-zero
representation for NULL pointers and 0.0 floating point values
(maybe mostly for historical reasons) it simply can't mandate
that more than one member of a static union is initialized to
a "zero" value.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

James Kuyper

9/13/2011 12:09:00 PM

0

On 09/13/2011 02:07 AM, Lung.S.Wu wrote:
> China Blue Corn Chipsæ?¼ 2011å¹´9æ??13æ?¥æ??æ??äº?UTC+8ä¸?å?1æ??34å??20ç§?寫é?ï¼?
>> In article
>> <8808246d-5605-4a5d...@glegroupsg2000goo.googlegroups.com>,
>> "Lung.S.Wu" <lung...@gmail.com> wrote:
>>
>>> From ANSI C definition, section 6.7.8, with UNION, it says that only the
>>> first named element will be initialized if it is not initialized explicitly.
>>> That means, such the code
>>> 1 typedef union {
>>> 2 char a;
>>> 3 short b;
>>> 4 int c;
>>> 5 } UU;
>>> 6
>>> 7 UU u;
>>> 8
>>> 9 int main(void)
>>> 10 {
>>> 11
>>> 12 return 0;
>>> 13 }
>>> Only the member u.a is initialized as 0, And others are undefined.
>>
>> If you want to depend on an initialisation, do so explicitly,
>> UU u = {.c = 0}
> Yes, This is C99 defined.
> My working environment cross GNU C, VS 2005/2008. VS does not support this kind of initialize.

Then another alternative is to make the member that you want initialized
to 0 be the first element in the union. This won't help, of course, if
the element you want 0-initialized is different in different contexts.
--
James Kuyper

robertwessel2@yahoo.com

9/13/2011 6:34:00 PM

0

On 13 Sep 2011 11:42:13 GMT, jt@toerring.de (Jens Thoms Toerring)
wrote:

>Ike Naar <ike@iceland.freeshell.org> wrote:
>> On 2011-09-13, Lung.S.Wu <lung.s.wu@gmail.com> wrote:
>> > Is there any system which defines NULL, floating point 0 not all bits-zero?
>
>> This is a FAQ.
>> http://c-faq.com/null/mach...
>
>And I remember to have read that on 286 and 386 machines without
>a math co-processor floating point operations were done via soft-
>ware emulation using a non-IEEE 754 format where 0.0 had a non-
>all-bits-zero representation (but I may mis-remember, never ha-
>ving used that kind of machines;-). And, while I have no expe-
>rience with embedded systems, I wouldn't be too surprised if
>one would find something similar on those. Furthermore, there
>is nothing that keeps people developing new architectures that
>don't use IEEE 754 or have non-zero NULL pointers...
>
>So it's not really so much the question if such machines exit
>but that, since the C standard doesn't require an all-bits-zero
>representation for NULL pointers and 0.0 floating point values
>(maybe mostly for historical reasons) it simply can't mandate
>that more than one member of a static union is initialized to
>a "zero" value.


While I certainly can't speak to all of the float formats ever
implemented on x86 software (and there were more than a few), MS's
main "pre-IEEE" format ("MBF" - ignoring the decimal float support in
some of their early products) did treat an all-bits-zero number as
zero (actually anything with a zero exponent field).

But the most common emulated FP format on x86 was, in fact, IEEE, just
done in software. The 8087 became available around the same time the
original IBM PC shipped (perhaps a year before), and the PC had a
coprocessor socket from day one (although it was not "officially" an
8087 socket at first). So the emulation libraries often had an option
for doing emulation, or actually using the coprocessor, if it was
present. At some point the emulation was actually moved into the OS
for MS products, and applications could just be coded with x87
instructions, and the OS would either emulate or just let the
instructions execute on the FPU.

Some folks did other things for performance if there was no
coprocessor, and there were even non-8087 compatible FPUs.

Ignoring that the 8087 predated the final IEEE math spec, and didn't
quite implement what we'd call IEEE math today (or at least even less
so than today's "8087s").

Of course many of the floating point formats have multiple zeros, but
MBF and IEEE, at least, will treat all-bits-zero as a zero.

Kenneth Brody

9/13/2011 8:30:00 PM

0

On 9/13/2011 1:34 AM, China Blue Corn Chips wrote:
> In article
> <8808246d-5605-4a5d-b8a2-ae7267769aed@glegroupsg2000goo.googlegroups.com>,
> "Lung.S.Wu"<lung.s.wu@gmail.com> wrote:
>
>> From ANSI C definition, section 6.7.8, with UNION, it says that only the
>> first named element will be initialized if it is not initialized explicitly.
>> That means, such the code
[...]
>> Only the member u.a is initialized as 0, And others are undefined.
>
> If you want to depend on an initialisation, do so explicitly,
> UU u = {.c = 0}
[...]

Question:

If one defines two different unions, consisting of the same members, but in
a different order, are they guaranteed to be equivalent?

union foo {
int i;
float f;
char *pt;
};

union bar {
char *pt;
int i;
float f;
};

Given 6.7.2.1p14, I would think so:

> A pointer to a union object, suitably converted, points to each of its
> members (or if a member is a bitfield, then to the unit in which it
> resides), and vice versa.

Of course, doing so, just to make the "correct" member be initialized to
zero, would be "unusual" to say the least.

--
Kenneth Brody

James Kuyper

9/13/2011 9:45:00 PM

0

On 09/13/2011 04:29 PM, Kenneth Brody wrote:
> On 9/13/2011 1:34 AM, China Blue Corn Chips wrote:
>> In article
>> <8808246d-5605-4a5d-b8a2-ae7267769aed@glegroupsg2000goo.googlegroups.com>,
>> "Lung.S.Wu"<lung.s.wu@gmail.com> wrote:
>>
>>> From ANSI C definition, section 6.7.8, with UNION, it says that only the
>>> first named element will be initialized if it is not initialized explicitly.
>>> That means, such the code
> [...]
>>> Only the member u.a is initialized as 0, And others are undefined.
>>
>> If you want to depend on an initialisation, do so explicitly,
>> UU u = {.c = 0}
> [...]
>
> Question:
>
> If one defines two different unions, consisting of the same members, but in
> a different order, are they guaranteed to be equivalent?
>
> union foo {
> int i;
> float f;
> char *pt;
> };
>
> union bar {
> char *pt;
> int i;
> float f;
> };
>
> Given 6.7.2.1p14, I would think so:
>
>> A pointer to a union object, suitably converted, points to each of its
>> members (or if a member is a bitfield, then to the unit in which it
>> resides), and vice versa.

That's quite plausible, but the standard makes no guarantees about it.
In particular, it does not say that union foo is compatible with union
bar. Among other implications is that a conforming implementation of C
is not required to consider the possibility that an a union foo* and a
union bar* might alias each other. Consider the following code:

void func(union foo *pf, union bar *pb)
{
for(; pf->i; pf++)
pf->i = pb->i;
}

A conforming implementation of C could optimize that code into the
equivalent of

void func(union foo *pf, union bar *pb)
{
int i = pb->i;
for(; pf->i; pf++)
pf->i = i;
}

Such an optimization would not be permissible if you changed "union bar"
to "union foo", because pb == pf might be true at some point during the
loop. That's a possibility that the implementation is not required to
take into consideration if pf and pb point to incompatible types.

> Of course, doing so, just to make the "correct" member be initialized to
> zero, would be "unusual" to say the least.

6.2.7p1:
"Moreover, two structure, union, or enumerated types declared in
separate translation units are compatible if their tags and members
satisfy the following requirements: If one is declared with a tag, the
other shall be declared with the same tag. If both are complete types,
then the following additional requirements apply: there shall be a
one-to-one correspondence between their members such that each pair of
corresponding members are declared with compatible types, and such that
if one member of a corresponding pair is declared with a name, the
other member is declared with the same name. For two structures,
corresponding members shall be declared in the same order. For two
structures or unions, corresponding bit-fields shall have the same widths."

Note that if "union foo" and "union bar" had been given the same tag in
different translation units, they would have be compatible. The
requirement that the members be declared in the same order applies only
to structures, not to unions or enumerations.

Here's one possible way to take advantage of that fact:

reorderable_union.h:
#ifndef REORDERABLE_H
#define REORDERABLE_H

union reorderable {
#if defined(INT_FIRST)
int i;
float f;
char *pt;
#elif defined(FLOAT_FIRST)
float f;
int i;
char *pt;
#else
char *pt;
int i;
float f;
#endif
};

#endif


Lung.S.wu

9/14/2011 2:32:00 AM

0

If each member will be cleared to zero. Will it be better with such code?

4 typedef union {
5 int i;
6 float f;
7 char *pt;
8 } bfoo;
9
10 union f {
11 char align[sizeof(bfoo)];
12 bfoo foo;
13 };
14
15 union f fff;