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


Groups > comp.lang.c > #158575

Re: Usage of union

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Usage of union
Date 2021-01-23 08:01 -0800
Organization A noiseless patient Spider
Message-ID <86zh0zmym1.fsf@linuxsc.com> (permalink)
References (2 earlier) <ru1lna$4nl$1@dont-email.me> <12db7b19-3ba3-4562-aee9-51531a52fcf3n@googlegroups.com> <ru1qs7$hht$1@dont-email.me> <86lfcnodl3.fsf@linuxsc.com> <8a82ee84-8053-491c-95a0-c4371cd65672n@googlegroups.com>

Show all headers | View raw


wij <wyniijj@gmail.com> writes:

> On Wednesday, 20 January 2021 at 23:03:38 UTC+8, Tim Rentsch wrote:
>
>> Lew Pitcher <lew.p...@digitalfreehold.ca> writes:
>>
>>> On Sun, 17 Jan 2021 08:41:29 -0800, wij wrote:
>>>
>>>> On Sunday, 17 January 2021 at 23:39:34 UTC+8,
>>>> james...@alumni.caltech.edu wrote:
>>>> ...
>>>>
>>>>> Note that, since the representation of types is not completely standard
>>>>> specified for any type, and is entirely implementation-defined for many
>>>>> types, there's virtually no portable way to take advantage of this
>>>>> fact.
>>>>> For example, there's no guarantee which bits of u16 are zeroed by
>>>>> execution of u8[0] = 0.  It could, for instance, be only the
>>>>> odd-numbered bits (though that would require a VERY unusual
>>>>> architecture).
>>>>
>>>> Understandable.  But:
>>>
>>> For context, here's the relevant part of the OP:
>>>
>>> //-----------------------------
>>> union {
>>> uint16 u16;  [understood to mean uint16_t]
>>> uint8 u8[2];  [understood to mean uint8_t]
>>> float flt;
>>> } u;
>>>
>>> void t() {
>>> u.u16=0;
>>>
>>> assert(u.u8[0]==0);
>>>
>>>
>>>> 1.  Will such a writing "assert(u.u8[0]==0);" cause UB?
>>>
>>> Yes.  The prior code does not store a value in u.u8[0]; u.u8[0],
>>> therefore, holds an indeterminate value.  [...]
>>
>> This is wrong.  The objects for u.u8[0] and u.u16 overlap.
>> After assigning to u.u16, u.u8[0] holds the first 8 bits of the
>> object representation of the value stored in u.u16.  We know
>> those bits must all be zero.  We also know that the abstract
>> value for the object representation (of u.u8[0]) of all bits
>> zero will be 0.  Thus the access is well defined and must yield
>> the value 0, so the assert(u.u8[0]==0) must succeed.  There is
>> no undefined behavior, or even the possibility of undefined
>> behavior.  (Note the "understood to mean" annotations in the
>> above quoted code.  Different types could mean different
>> conclusions.)
>>
>> The above statements apply to C, but not to C++.  In C++ the
>> rules for union members are different, and the access of u.u8[0]
>> is undefined behavior,
>
> C++ is a top-down language (developed by elites).  But it also has
> to build on C or something C has provided, because at least, low
> level things to my knowledge are mostly governed by C or C
> compatible way.
> So, if no surprise, I think 'traditional' C way of using union is
> fine, otherwise the compiler should consider give warnings or
> errors.

The C++ standards group has decided that the rules for union
member access do not match the C rules.  That's just how it is.

As for giving some sort of warning/error, that is not workable in
any practical sense, because the question is undecidable, and
even fairly common real-world scenarios are too difficult to
diagnose effectively.

>> ...because it is being accessed outside of
>> its lifetime.
>
> In lawyer (language specialist) way of speaking, object uint8_t is
> never created , so no such thing as 'lifetime'.  (kidding)

The C++ standard gives specific rules for the lifetimes of
objects in general and of union member objects in particular, and
those rules don't match the C rules for object lifetimes in the
particular case of union member objects.

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