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: Fri, 06 Jun 2025 17:44:14 -0700
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <8634ccs7mp.fsf@linuxsc.com>
References: <86plgo7ahu.fsf@linuxsc.com> <20250505111213.00004b55@yahoo.com> <20250505120145.000014f8@yahoo.com> <87jz6uhkgo.fsf@nosuchdomain.example.com> <1019jfg$3rqk1$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Sat, 07 Jun 2025 02:44:15 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ef8e131b4cc96276195033b53270f984"; logging-data="2717901"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19aeEc6Lu5voU17niKmVayThpynYY4L1WU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:qxGdTXR/wS+Pl2WMRdZcrkL2AzE= sha1:Dx3t/C2NdZi4sx8vh1eGGBssfSQ=
Xref: csiph.com comp.lang.c:393739
Andrey Tarasevich writes:
> On Tue 5/6/2025 10:36 AM, Waldek Hebisch wrote:
>
>> 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.
>
> Exactly. I thought the nature of the corrections I made (i.e. the
> deliberate usage of comma operator) would be strikingly obvious to the
> participants of the thread. But alas...
My own reaction is that the changes were not by themselves
strikingly obvious. But in combination with the explicit
statement that "This version has no UB" it seems obvious
enough.