Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Regarding assignment to struct Date: Tue, 06 May 2025 13:06:02 -0700 Organization: None to speak of Lines: 42 Message-ID: <87msbpfqs5.fsf@nosuchdomain.example.com> References: <86plgo7ahu.fsf@linuxsc.com> <20250505111213.00004b55@yahoo.com> <20250505120145.000014f8@yahoo.com> <87jz6uhkgo.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Tue, 06 May 2025 22:06:05 +0200 (CEST) Injection-Info: dont-email.me; posting-host="200f1b2c855e9fa31fd1080bfdd034d6"; logging-data="3863271"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18htdiEBfv70/yeg/gnDWDd" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:Mslwa2iAZEdgsbEU4r0ckg6w2TM= sha1:HxkPAXnqM6z44a1Bg67+pVgJuck= Xref: csiph.com comp.lang.c:393215 antispam@fricas.org (Waldek Hebisch) writes: > Keith Thompson wrote: >> Andrey Tarasevich writes: >> [...] >>> #include >>> >>> 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. >> >> N3096 6.2.4p2: "If a pointer value is used in an evaluation after >> the object the pointer points to (or just past) reaches the end of >> its lifetime, the behavior is undefined. The representation of a >> pointer object becomes indeterminate when the object the pointer >> points to (or just past) reaches the end of its lifetime." > > Note commas above. Assignment to pc and call to printf are parts > of a single expression, so use of pc is within lifetime of the > temporary object. Right, I didn't see the commas and assumed semicolons. Changing the formatting would have made the point much clearer. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */