Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10156

Re: UNION global variabl initialize

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: UNION global variabl initialize
Date 2011-09-12 17:08 +0000
Organization Freie Universitaet Berlin
Message-ID <9d6skcF5t1U1@mid.uni-berlin.de> (permalink)
References <8808246d-5605-4a5d-b8a2-ae7267769aed@glegroupsg2000goo.googlegroups.com>

Show all headers | View raw


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://toerring.de

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

UNION global variabl initialize "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-12 09:38 -0700
  Re: UNION global variabl initialize jt@toerring.de (Jens Thoms Toerring) - 2011-09-12 17:08 +0000
  Re: UNION global variabl initialize Kenneth Brody <kenbrody@spamcop.net> - 2011-09-12 15:02 -0400
    Re: UNION global variabl initialize "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-12 22:10 -0700
      Re: UNION global variabl initialize Ike Naar <ike@iceland.freeshell.org> - 2011-09-13 07:10 +0000
        Re: UNION global variabl initialize jt@toerring.de (Jens Thoms Toerring) - 2011-09-13 11:42 +0000
          Re: UNION global variabl initialize Robert Wessel <robertwessel2@yahoo.com> - 2011-09-13 13:34 -0500
  Re: UNION global variabl initialize China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-12 22:34 -0700
    Re: UNION global variabl initialize "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-12 23:07 -0700
      Re: UNION global variabl initialize China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-12 23:35 -0700
      Re: UNION global variabl initialize James Kuyper <jameskuyper@verizon.net> - 2011-09-13 08:09 -0400
    Order of UNION members (was Re: UNION global variabl initialize) Kenneth Brody <kenbrody@spamcop.net> - 2011-09-13 16:29 -0400
      Re: Order of UNION members (was Re: UNION global variabl initialize) James Kuyper <jameskuyper@verizon.net> - 2011-09-13 17:45 -0400
        Re: Order of UNION members (was Re: UNION global variabl initialize) "Lung.S.Wu" <lung.s.wu@gmail.com> - 2011-09-13 19:32 -0700
          Re: Order of UNION members (was Re: UNION global variabl initialize) James Kuyper <jameskuyper@verizon.net> - 2011-09-14 06:59 -0400
            Re: Order of UNION members (was Re: UNION global variabl initialize) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-14 15:08 +0100
        Re: Order of UNION members (was Re: UNION global variabl initialize) Kenneth Brody <kenbrody@spamcop.net> - 2011-09-14 12:50 -0400
          Re: Order of UNION members (was Re: UNION global variabl initialize) James Kuyper <jameskuyper@verizon.net> - 2011-09-14 13:21 -0400

csiph-web