I read this big too broad question and ran into some UBs that I did not know about before.
The main reason I see UB from time to time changes a variable twice between two points in a sequence. Things like: x = x++or z = y++ + ++y;. Reading that changing a variable twice between two points in a sequence is UB helped me understand what the main reason is in these cases.
But what about things like a bit-shift with negatives? ( int x = 8 << -1) Is there a rule that can explain this, or should I remember this as a unique UB opportunity?
I looked here and in the Integer Overflow section . I found that a bit-shift with negatives was written, but I don’t understand why they relate to. When it intshifts too much, an overflow occurs, but the IMO offset is negative - it’s just UB, and the problem is not in bits that “exceed the edge” ...
Also looked here, but this did not answer my question:
Integer promotions are executed for each of the operands. The result type is the advanced left operand. If the value of the operand on the right is negative or greater than or equal to the width of the advanced left operand, the behavior is undefined .
So my questions are:
- In particular, a bit-shift with negative values is considered a whole overflow, and if so, why?
- , ?
- () , ?