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


Groups > comp.lang.c > #14462

Re: Repeated types in union

Message-ID <4EE3E711.7040406@loria.fr> (permalink)
Date 2011-12-11 00:11 +0100
From Jens Gustedt <jens.gustedt@loria.fr>
Newsgroups comp.lang.c
Subject Re: Repeated types in union
References <jc0l4g$cvr$1@speranza.aioe.org>
Organization Guest of ProXad - France

Show all headers | View raw


Am 12/10/2011 11:06 PM, schrieb Edward Rutherford:
> Is the following code an undefined behavior?
> 
> 
>  union {
>         int a;
>         int b; 
>  } u;
>  u.a = 3;
>  printf("%d\n", u.b);

not that I see. Acessing a different member than the one was last stored
is only undefined behavior if the bit pattern results in a trap
representation for the new type.

If the first member has padding bytes that the other type uses for its
data representation, the value of these bytes is *unspecified* which is
not the same thing as UB.

In any case, none of these things can happen for your example.

Jens

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


Thread

Repeated types in union Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-12-10 22:06 +0000
  Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-11 00:11 +0100
  Re: Repeated types in union Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-10 18:14 -0500
    Re: Repeated types in union Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-12-12 20:09 +0000
      Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-12 23:45 +0100
      Re: Repeated types in union Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-12 19:48 -0500
        Re: Repeated types in union "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-12-13 15:17 -0800
          Re: Repeated types in union Keith Thompson <kst-u@mib.org> - 2011-12-13 15:29 -0800
          Re: Repeated types in union James Kuyper <jameskuyper@verizon.net> - 2011-12-13 18:30 -0500
          Re: Repeated types in union Noob <root@127.0.0.1> - 2011-12-14 17:40 +0100
          Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:28 -0800
        Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 15:04 -0800
    Re: Repeated types in union ralph <nt_consulting64@yahoo.net> - 2011-12-12 14:50 -0600
    Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 14:53 -0800
  Re: Repeated types in union Barry Schwarz <schwarzb@dqel.com> - 2011-12-10 22:04 -0800
    Re: Repeated types in union "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-12-12 09:49 -0800
      Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-12 20:09 +0100
        Re: Repeated types in union "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-12-13 15:10 -0800
          Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-14 00:24 +0100
          Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:46 -0800
      Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:42 -0800
    Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:36 -0800

csiph-web