Sujet : Re: Regarding assignment to struct
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 29. May 2025, 21:56:38
Autres entêtes
Organisation : None to speak of
Message-ID : <87wm9zdtkp.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Gnus/5.13 (Gnus v5.13)
Andrey Tarasevich <
noone@noone.net> writes:
On Mon 5/5/2025 1:27 PM, Keith Thompson wrote:
Andrey Tarasevich <noone@noone.net> writes:
[...]
#include <stdio.h>
>
struct S { int a[10]; };
>
int main()
{
struct S a, b = { 0 };
int *pa, *pb, *pc;
>
pa = &a.a[5],
pb = &b.a[5],
pc = &(a = b).a[5],
printf("%p %p %p\n", (void *) pa, (void *) pb, (void *) pc);
}
>
This version has no UB.
I believe it does. pc points to an element of an object with
temporary lifetime. The value of pc is then used after the object
it points to has reached the end of its lifetime. At that point,
pc has an indeterminate value.
>
Nope. Nowhere in this code the value of `pc` is used beyond the
lifetime of the object with temporary lifetime.
>
Pay attention to the fact that the last 4 lines in above code is a
single expression joined by a comma operator, which is the whole point
of the corrections that differentiate it from the original version.
Yes, yes, the fact that the code uses comma operators rather
than semicolons was pointed out and discussed several weeks ago.
I initially missed that change, it was pointed out, I acknowledged
it, and we moved on.
If you're going to reply to an old article, please read the thread
before posting.
In response to something else you recently wrote in this thread,
the change from semicolons to commas was not at all obvious.
The code is formatted in a way that strongly suggests statements
with ending semicolons. Several of us missed the commas on first
reading, having seen nearly identical code earlier in the thread
with no acknowledgement that the code had been modified. I simply
assumed that it was the same code, and didn't bother to re-read it
character by character.
[...]
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */