Liste des Groupes | Revenir à cl c |
David Brown <david.brown@hesbynett.no> writes:Of course. In fact, if someone presented such code for review (and assuming I noticed the commas!) I'd have to consider whether it was done maliciously, intentionally deceptively, due to incompetence, or smart-arse coding. In all my C coding experience, I can't recall ever coming across a single situation when I thought the use of the comma operator was appropriate in the kind of code I work with.On 06/05/2025 19:36, Waldek Hebisch wrote:That would get an immediate downcheck during review for exactlyKeith 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.
>
I must admit I had not noticed that detail.
that reason.
Les messages affichés proviennent d'usenet.