I've found something in our codebase that, even if it has never failed, doesn't look "right" to me.
I've trimmed the code down to something akin to a linked list (the data structures are much more complex than that).
Node * node = firstNode();
while( moreNodesAwaiting() )
{
Node * newnode = giveMeAnotherNode();
node = node->next = newnode ; // <-- This is what I don't like.
}
I'm not sure if undefined behaviour applies here. We're modifying both the data structure and the pointer to it between sequence points, but that's not the same as modifying the value twice.
Also, if the compiler can evaluate elements of an expression in whatever order, does it mean that it can evaluate node->next BEFORE the assignment takes place?
Just in case, I've changed it to something like:
node->next = newnode ;
node = node->next ;
which also emphasizes the fact that we traverse the list.
5.17 Assignment and compound assignment operators In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression.
In highly-object-oriented languages, double assignment results in the same object being assigned to multiple variables, so changes in one variable are reflected in the other.
Multiple assignment in Python: Assign multiple values or the same value to multiple variables. In Python, use the = operator to assign values to variables. You can assign values to multiple variables on one line.
In Python, assignment statements are not expressions and thus do not have a value. Instead, chained assignments are a series of statements with multiple targets for a single expression.
Assignments associate right-to-left. In other words:
a = b = c;
is parsed as
a = (b = c);
and this order is guaranteed by the standard.
That is, for primitive types, it will assign c
into both b
and a
. For non-primitive types, it will call a.operator= (b.operator= (c))
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With