Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.c > #393739

Re: Regarding assignment to struct

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Regarding assignment to struct
Date 2025-06-06 17:44 -0700
Organization A noiseless patient Spider
Message-ID <8634ccs7mp.fsf@linuxsc.com> (permalink)
References (6 earlier) <20250505120145.000014f8@yahoo.com> <vvame5$ppqp$1@dont-email.me> <87jz6uhkgo.fsf@nosuchdomain.example.com> <vvdhb2$3m8gn$1@paganini.bofh.team> <1019jfg$3rqk1$4@dont-email.me>

Show all headers | View raw


Andrey Tarasevich <noone@noone.net> writes:

> On Tue 5/6/2025 10:36 AM, Waldek Hebisch wrote:
>
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> 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.
>>>
>>> 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.

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


Thread

Re: Regarding assignment to struct Andrey Tarasevich <noone@noone.net> - 2025-05-29 05:21 -0700
  Re: Regarding assignment to struct Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-05-29 16:43 +0200
  Re: Regarding assignment to struct Tim Rentsch <tr.17687@z991.linuxsc.com> - 2025-06-06 17:44 -0700

csiph-web