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


Groups > comp.std.c > #6163

Re: Assignment between union object members of incompatible types

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.std.c
Subject Re: Assignment between union object members of incompatible types
Date 2020-12-01 02:49 -0800
Organization A noiseless patient Spider
Message-ID <86a6uxpzrt.fsf@linuxsc.com> (permalink)
References <272ec269-16e7-4e06-b8a8-e5f6534e66a8n@googlegroups.com>

Show all headers | View raw


Ian Abbott <ijabbott63@gmail.com> writes:

> A question (not posted by myself) from https://stackoverflow.com/questions/65077630 :
>
> Consider the following:
>
> union { int i; char c; } x = {0};
> x.c = x.i;
>
> Does the assignment x.c = x.i result in undefined behavior?
>
> C18 6.15.16.1/3 says:

As elsewhere noted, the reference is 6.5.16.1 paragraph 3.

> | If the value being stored in an object is read from another object
> | that overlaps in any way the storage of the first object, then the
> | overlap shall be exact and the two objects shall have qualified or
> | unqualified versions of a compatible type;  otherwise, the behavior is
> | undefined.
>
> The objects x.c and x.i overlap, but have incompatible types, so on
> first glance it appears to be UB.

The value to be stored is read from object x.i.  This value is
being stored in object x.c.

The object x.i overlaps with object x.c.

The overlap is not exact (probably).  The two types involved are
not qualified or unqualified versions of a compatible type.

The assignment has undefined behavior.  No doubt about it.

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


Thread

Assignment between union object members of incompatible types Ian Abbott <ijabbott63@gmail.com> - 2020-11-30 10:44 -0800
  Re: Assignment between union object members of incompatible types James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-11-30 14:45 -0500
  Re: Assignment between union object members of incompatible types Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-11-30 12:20 -0800
    Re: Assignment between union object members of incompatible types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-01 03:39 -0800
      Re: Assignment between union object members of incompatible types Francis Glassborow <francis.glassborow@btinternet.com> - 2020-12-05 14:27 +0000
        Re: Assignment between union object members of incompatible types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-10 08:42 -0700
  Re: Assignment between union object members of incompatible types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-01 02:49 -0800

csiph-web