Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #158481
| Path | csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: Usage of union |
| Date | Wed, 20 Jan 2021 07:03:20 -0800 |
| Organization | A noiseless patient Spider |
| Lines | 55 |
| Message-ID | <86lfcnodl3.fsf@linuxsc.com> (permalink) |
| References | <0f20fca0-ccf0-4f43-99f7-d2ed20d9f3b7n@googlegroups.com> <d1f99f5b-39e5-41fb-b1b6-a58493736a71n@googlegroups.com> <ru1lna$4nl$1@dont-email.me> <12db7b19-3ba3-4562-aee9-51531a52fcf3n@googlegroups.com> <ru1qs7$hht$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | reader02.eternal-september.org; posting-host="b1fd427021f152ca5ff6680f6ccee516"; logging-data="8211"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19iDIB+bEdbLMXef9KFDkFiS1q/h8uDNmY=" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:Zrzh8zob3yDI3KPmsRKfN0Yjqm4= sha1:8NNoEtLGKXzAO4yN7/48amFD1jw= |
| Xref | csiph.com comp.lang.c:158481 |
Show key headers only | View raw
Lew Pitcher <lew.pitcher@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, because it is being accessed outside of
its lifetime.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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