UNION global variabl initialize

L

Lung.S.Wu

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?
 
J

Jens Thoms Toerring

Lung.S.Wu said:
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
 
L

Lung.S.Wu

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

Lung.S.Wu

China Blue Corn Chipsæ–¼ 2011å¹´9月13日星期二UTC+8下åˆ1時34分20秒寫é“:
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.
 
J

James Kuyper

China Blue Corn Chipsæ–¼ 2011å¹´9月13日星期二UTC+8下åˆ1時34分20秒寫é“:
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.
 
J

James Kuyper

Lung.S.Wu said:
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
 
L

Lung.S.Wu

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;
 
J

James Kuyper

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

The only member guaranteed to be "cleared to zero" when a union object
is default-initialized is the first member; that's the point of this thread.
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;

The standard guarantees that default initialization of fff sets every
element of fff.align to 0. This is sufficient to ensure that
fff.foo.i==0, but provides no similar guarantees for fff.foo.f or
fff.foo.pt. This doesn't seem like any substantial improvement over the
guarantees that you get for a default-initialized object of type bfoo.
 
B

Ben Bacarisse

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

The only member guaranteed to be "cleared to zero" when a union object
is default-initialized is the first member; that's the point of this thread.
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;

The standard guarantees that default initialization of fff sets every
element of fff.align to 0. This is sufficient to ensure that
fff.foo.i==0, but provides no similar guarantees for fff.foo.f or
fff.foo.pt. This doesn't seem like any substantial improvement over the
guarantees that you get for a default-initialized object of type bfoo.

The main one (not great, I admit) is that if sizeof(char *) or
sizeof(float) is > sizeof(int) they too get zero-bit initialised. If
you know that zero-bits equals zero, that may be enough for you. I an
not sure there's much use for this, but it is a small advantage.
 
J

James Kuyper

On 09/13/2011 04:29 PM, Kenneth Brody wrote: [...]
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
[...]

Okay, but I think of that as one of those generic "you lied to the compiler
when you told it that these two pointers never point to the same object"
type of situations. :)

But I didn't; that would be equivalent to using the 'restrict' keyword,
which would enable the same optimization, regardless of the type of pb.
All that I promised is that the pointers never point to objects of the
same type. It is section 6.5p7 which, as a result of the fact that these
pointers do not point at compatible types, implies that they therefore
cannot point at the same object.
I find it hard to believe that the Standard would allow the following:

===== fileA.C

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

...

===== fileB.C

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

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

=====

And have unions "foo" and "bar" in fileB.C not be equivalent.

I agree that they are "equivalent" in many ways. However, they're not
required to be compatible, and as a result there's a number of
associated guarantees in the standard that do NOT apply - the most
important that I can think of being the aliasing issue I've already
raised. As a result, you cannot safely use one in place of the other,
which means that they are not equivalent for all purposes.
... And, if
fileB's bar is equivalent to its foo, and its foo is equivalent to fileA's
foo, then fileB's foo must also be equivalent to fileA's foo.

And, if fileB's foo is removed, its bar still remains equivalent.

That's not to say you can convert a "foo*" to a "bar*" without an explicit
cast. It's just that the cast is perfectly legal, and invokes no
non-standard behavior.

There's nothing in the standard that guarantees that the unions have the
same alignment requirements - though there's obviously no reason for
them being different. In principle, therefore, even the cast is not
guaranteed to be safe.

However, the more serious problem is that dereferencing the resulting
pointer value violates 6.5p7 because the standard doesn't say that foo
and bar are compatible types.

....
Again, I can't see actually doing such a thing just to default-initialize a
different "first" member.

Agreed. Explicitly initializing the desired member seems the more
appropriate solution, even in C90, where you have to do so outside the
initializer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,818
Messages
2,569,736
Members
45,709
Latest member
DRISenaida

Latest Threads

Top