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


Groups > comp.lang.c > #158478

Re: Usage of union

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Usage of union
Date 2021-01-20 05:56 -0800
Organization A noiseless patient Spider
Message-ID <86turbogog.fsf@linuxsc.com> (permalink)
References <0f20fca0-ccf0-4f43-99f7-d2ed20d9f3b7n@googlegroups.com> <ru1mk1$asv$1@dont-email.me> <ru1nv2$k8v$1@dont-email.me>

Show all headers | View raw


Andrey Tarasevich <andreytarasevich@hotmail.com> writes:

[context: does the following code have undefined behavior?

        union {
          uint16_t u16;
          uint8_t  u8[2];
          float    flt;
        } u;
        
        void t() {
          u.u16=0;
        
          assert(u.u8[0]==0);
          assert(u.u8[1]==0);
        
        };

(end context)]

> [In C] there's no immediate UB when reading `u8`.  In general
> case the potential for UB is still there [because of trap
> representations], but with uint8_t` everything should be fine
> since `uint8_t` is not supposed to have trap representations.
> (BTW, it seems that this is not explicitly spelled out in C
> standard.)

All of the exact-width integer types [u]intN_t, including uint8_t,
cannot have trap representations.  The C standard doesn't say this
directly, but it follows pretty immediately from the properties of
exact-width integer types and from the definitions of object
representation and trap representation.  (For signed exact-width
types intN_t, section 7.20.2.1 provides a crucial piece of that
deduction, which is easy to miss if one doesn't know to look for
it.)

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


Thread

Usage of union wij <wyniijj@gmail.com> - 2021-01-17 06:09 -0800
  Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-17 06:18 -0800
    Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 10:39 -0500
      Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-17 08:41 -0800
        Re: Usage of union Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-01-17 17:07 +0000
          Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:29 -0500
            Re: Usage of union Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-01-17 17:47 +0000
            Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 14:22 -0500
          Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-20 07:03 -0800
            Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-20 09:33 -0800
              Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-23 08:01 -0800
              Re: Usage of union Jorgen Grahn <grahn+nntp@snipabacken.se> - 2021-01-26 20:28 +0000
                Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-27 03:15 -0800
        Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:19 -0500
      Re: Usage of union Bonita Montero <Bonita.Montero@gmail.com> - 2021-01-20 19:24 +0100
        Re: Usage of union Christian Hanné <the.hanne@gmail.com> - 2021-01-20 19:31 +0100
  Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-17 07:54 -0800
    Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-17 08:17 -0800
      Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-17 09:07 -0800
      Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:32 -0500
      Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-20 05:56 -0800
        Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-20 09:52 -0800
    Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-20 06:09 -0800
      Re: Usage of union Bonita Montero <Bonita.Montero@gmail.com> - 2021-01-20 19:41 +0100
        Re: Usage of union scott@slp53.sl.home (Scott Lurndal) - 2021-01-20 18:49 +0000
          Re: Usage of union Bonita Montero <Bonita.Montero@gmail.com> - 2021-01-20 20:09 +0100
            Re: Usage of union scott@slp53.sl.home (Scott Lurndal) - 2021-01-20 19:25 +0000
            Re: Usage of union David Brown <david.brown@hesbynett.no> - 2021-01-20 20:29 +0100
        Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-20 11:36 -0800
  Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:14 -0500
  Re: Usage of union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-17 17:17 +0000

csiph-web