Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: Regarding assignment to struct Date: Thu, 08 May 2025 12:45:58 -0700 Organization: A noiseless patient Spider Lines: 80 Message-ID: <86y0v6zy15.fsf@linuxsc.com> References: <86plgo7ahu.fsf@linuxsc.com> <87seljh3a3.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Thu, 08 May 2025 21:46:05 +0200 (CEST) Injection-Info: dont-email.me; posting-host="8ae053696287f1619c3fbdcfaf84f4fc"; logging-data="2207237"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2VN3b2qIlSPl2TGTd1LXHM+wAtQS0pX0=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:eLN2aoHlX8vtACwWVGFxjOaGyt8= sha1:EjTW0bYidPVMTg2zI3fxFFQ1bi8= Xref: csiph.com comp.lang.c:393260 Andrey Tarasevich writes: > On Mon 5/5/2025 1:26 AM, Keith Thompson wrote: > >>> I wondering what the last sentence is intended to mean ("... need not >>> have a unique address"). At the first sight, the intent seems to be >>> obvious: it simply says that such temporary objects might repeatedly >>> appear (and disappear) at the same location in storage, which is a >>> natural thing to expect. >> >> You snipped this: "Any attempt to modify an object with temporary >> lifetime results in undefined behavior.". Which means, I think, >> that an implementation that shared storage for "such an object" >> with something else probably isn't going to cause problems for any >> code with defined behavior. > > It is going to cause problems, if the code relies on the address > identity of the object, assuming the standard intends to provide such > guarantees. > >> Though I can imagine the possibility of code that modifies `a` and >> reads via `pc` within the same full expression. > > That's easy (in the context of declarations from my previous example): > > pc = &(a = b).a[5], a.a[5] = 42, printf("%d\n", *pc); > > As one would expect, this produces different output in GCC and Clang > for the reasons I already described. > >> But unless I've somehow missed it, the "Such an object need not >> have a unique address." wording doesn't appear on that web page or >> in my copy of n1570.pdf. C17 does add these two sentences: >> >> An object with temporary lifetime behaves as if it were declared >> with the type of its value for the purposes of effective type. Such >> an object need not have a unique address. >> >> Normally any two objects with overlapping lifetime must have distinct >> addresses. This addition, I think, gives compilers permission to have >> temporary lifetime objects overlap with other existing objects, but not >> to have a modification to one object affect the value of the other >> (unless the modification invokes UB, of course). > > If so, that would be extremely underspecified. A mere "such an object > need not have a unique address" is insufficient to fully convey the > permission to overlap existing named objects. I don't see why you say that. The statement says objects with temporary lifetime need not have a unique address. In the absence of any other statement on the subject, this statement admits the inference that an object with temporary lifetime might have the same address as any other object. Removing the constraint (that the addresses of those objects must be distinct from the addresses of all other objects), /and doing nothing else/, can only mean that the addresses of such objects might match the address of any other object in the environment. If you think there should be a non-normative footnote explaining that point, I expect I would vote in favor of that, but as far as normative text goes I don't see any fuzziness about what is allowed under the existing wording. > And that's probably what led to difference in interpretation > between GCC and Clang. I suspect the implication actually goes the other way. It is because what gcc has done (past tense) violates the rules of the C11 standard that someone had the bright idea that the C standard should be changed to allow this stupidity. > Modification of the temporary is "prohibited" (as UB), but > modification of the overlapped named object is not. The > consequences can be quite surprising. In my view the problem is not that what is allowed is unclear, but that the whole idea of possibly overlapping objects is a crock. It's a sad statement on the quality of gcc that it does the wrong thing even when -std=c11 and -pedantic are given as compilation options. Bleah.