A
ais523
I've been wondering more about Undefined Behaviour, and the way in
which (i=i++)-like examples can be 'corrected' so they mean something
defined. This was particularly inspired by a line in some computer-
generated code, whose essence was as follows:
void func()
{
int a, b, *p;
a=b=0;
p=&b;
*p=1+((p=&a),2);
}
Here, the variable actually being assigned to depends on the RHS of
the assignment; but the comma introduces a sequence point, so I think
this is a defined unambiguous assignment to a. (I'm not sure, though:
this is why I'm asking c.l.c.) Some more examples along similar lines
for statements for which I'm not clear about defined/undefined/
unspecified:
c=(a++,b)+(b++,a);
The question here is whether the implementation is forced to evaluate
the two parenthesised groups sequentially due to the sequence points
in them. I think that this line might be UB, because of the
possibility of incrementing both variables first and then adding the
new values of a and b.
a=(a++,a);
My guess about this one is that it isn't UB because the comma forces
the increment to happen before the assignment, leaving the line
equivalent to ++a;.
So my question is: which of these examples are UB, and why?
which (i=i++)-like examples can be 'corrected' so they mean something
defined. This was particularly inspired by a line in some computer-
generated code, whose essence was as follows:
void func()
{
int a, b, *p;
a=b=0;
p=&b;
*p=1+((p=&a),2);
}
Here, the variable actually being assigned to depends on the RHS of
the assignment; but the comma introduces a sequence point, so I think
this is a defined unambiguous assignment to a. (I'm not sure, though:
this is why I'm asking c.l.c.) Some more examples along similar lines
for statements for which I'm not clear about defined/undefined/
unspecified:
c=(a++,b)+(b++,a);
The question here is whether the implementation is forced to evaluate
the two parenthesised groups sequentially due to the sequence points
in them. I think that this line might be UB, because of the
possibility of incrementing both variables first and then adding the
new values of a and b.
a=(a++,a);
My guess about this one is that it isn't UB because the comma forces
the increment to happen before the assignment, leaving the line
equivalent to ++a;.
So my question is: which of these examples are UB, and why?