Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > UNION global variabl initialize

Reply
Thread Tools

UNION global variabl initialize

 
 
Lung.S.Wu
Guest
Posts: n/a
 
      09-12-2011
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?
 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      09-12-2011
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.


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 ___
\__________________________ http://toerring.de
 
Reply With Quote
 
 
 
 
Lung.S.Wu
Guest
Posts: n/a
 
      09-13-2011
Yes, agree with you.
Is there any system which defines NULL, floating point 0 not all bits-zero?
 
Reply With Quote
 
Lung.S.Wu
Guest
Posts: n/a
 
      09-13-2011
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.
>
>
> >
> > 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?

>
> --
> I remember finding out about you, |With the nutty taste of wild hickory nuts.
> Everyday my mind is all around you,| I'm whoever you want me to be.
> Looking out from my lonely room | Annoying Usenet one post at a time.
> Day after day. | At least I can stay in character.
> Celle est une langue. C'est francais et tres, tres sexuel.


 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      09-13-2011
On 2011-09-13, Lung.S.Wu <> wrote:
> Is there any system which defines NULL, floating point 0 not all bits-zero?


This is a FAQ.
http://c-faq.com/null/machexamp.html
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-13-2011
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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-13-2011
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->,
>> "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

> [...]
>>> 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


 
Reply With Quote
 
Lung.S.Wu
Guest
Posts: n/a
 
      09-14-2011
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;
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      09-14-2011
On 09/13/2011 10:32 PM, Lung.S.Wu wrote:
> 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.
--
James Kuyper
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-14-2011
James Kuyper <> writes:

> On 09/13/2011 10:32 PM, Lung.S.Wu wrote:
>> 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.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initialize global variable before any other global variables jubelbrus C++ 5 07-20-2007 06:38 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
How to initialize a member of union type alexwu C++ 2 09-12-2004 11:27 PM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57