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


Groups > comp.lang.c > #10214

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

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Order of UNION members (was Re: UNION global variabl initialize)
Date 2011-09-14 06:59 -0400
Organization A noiseless patient Spider
Message-ID <j4q1ds$fb7$1@dont-email.me> (permalink)
References <8808246d-5605-4a5d-b8a2-ae7267769aed@glegroupsg2000goo.googlegroups.com> <chine.bleu-E5236F.22342012092011@news.eternal-september.org> <1omdnQdZDazPIPLTnZ2dnUVZ_rGdnZ2d@bestweb.net> <4E6FCEF5.20608@verizon.net> <46f7e7d5-814d-427c-aa89-084e28f387d2@glegroupsg2000goo.googlegroups.com>

Show all headers | View raw


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

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