Undefined behavior or false positive

I (essentially) met the following in the wild

x = x = 5;

which explicitly compiles in an earlier version of gcc (generates a warning under gcc 4.5.1). As far as I can tell, a warning is generated by a dot-Sequence. So my question is, does this violate the wording in the standard about manipulating variables between points in a sequence (i.e., this behavior is undefined for the specification) or is gcc false positive (i.e., behavior is determined by specification)? The formulation of sequence points is a bit complicated.

I said essentially, because what I actually came across (in larger terms) was

x[0][0] = x[0][0] = 5;

but I did not think that this was essential for the warning (please correct me if this is so, and not what I supposed, the essence of the problem).

+3
source share
1 answer

Assuming it xhas a built-in type, it assigns xthe sequence twice without an intermediate point that all you need to know. The fact that both assignments have the same value (5) and can theoretically be optimized in one task (if xnot mutable) does not exist here.

, , "" - , , . , const const - , , UB, , . , , , , , .

x[0][0] = x[0][i] = 5;, , ( ) i == 0, i.

, - , : -)

, , . - , , , x[0][0] = 5, x[0][i] = 5. .

+5

Source: https://habr.com/ru/post/1773739/


All Articles