UNION global variabl initialize

Discussion in 'C Programming' started by Lung.S.Wu, Sep 12, 2011.

  1. Lung.S.Wu

    Lung.S.Wu Guest

    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?
     
    Lung.S.Wu, Sep 12, 2011
    #1
    1. Advertising

  2. 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
     
    Jens Thoms Toerring, Sep 12, 2011
    #2
    1. Advertising

  3. Lung.S.Wu

    Lung.S.Wu Guest

    Yes, agree with you.
    Is there any system which defines NULL, floating point 0 not all bits-zero?
     
    Lung.S.Wu, Sep 13, 2011
    #3
  4. Lung.S.Wu

    Lung.S.Wu Guest

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

    >
    > 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.
     
    Lung.S.Wu, Sep 13, 2011
    #4
  5. Lung.S.Wu

    Ike Naar Guest

    Ike Naar, Sep 13, 2011
    #5
  6. Lung.S.Wu

    James Kuyper Guest

    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
    >> <>,
    >> "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.

    >>
    >> 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
     
    James Kuyper, Sep 13, 2011
    #6
  7. Lung.S.Wu

    James Kuyper Guest

    Re: Order of UNION members (was Re: UNION global variabl initialize)

    On 09/13/2011 04:29 PM, Kenneth Brody wrote:
    > On 9/13/2011 1:34 AM, China Blue Corn Chips wrote:
    >> In article
    >> <>,
    >> "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
     
    James Kuyper, Sep 13, 2011
    #7
  8. Lung.S.Wu

    Lung.S.Wu Guest

    Re: Order of UNION members (was Re: UNION global variabl initialize)

    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;
     
    Lung.S.Wu, Sep 14, 2011
    #8
  9. Lung.S.Wu

    James Kuyper Guest

    Re: Order of UNION members (was Re: UNION global variabl initialize)

    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
     
    James Kuyper, Sep 14, 2011
    #9
  10. Re: Order of UNION members (was Re: UNION global variabl initialize)

    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.
     
    Ben Bacarisse, Sep 14, 2011
    #10
  11. Lung.S.Wu

    James Kuyper Guest

    Re: Order of UNION members (was Re: UNION global variabl initialize)

    On 09/14/2011 12:50 PM, Kenneth Brody wrote:
    > On 9/13/2011 5:45 PM, James Kuyper wrote:
    >> 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.

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

    >
    > 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.
     
    James Kuyper, Sep 14, 2011
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    669
    Matt Garman
    Apr 25, 2004
  2. alexwu
    Replies:
    2
    Views:
    6,412
    Rob Williscroft
    Sep 13, 2004
  3. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    876
    Chris Torek
    Apr 26, 2004
  4. vib
    Replies:
    18
    Views:
    657
    Michael Wojcik
    Jun 14, 2005
  5. jubelbrus
    Replies:
    5
    Views:
    621
    JohnQ
    Jul 20, 2007
Loading...

Share This Page