I’m reading an interesting article A Guide to Undefined Behavior in C and C++, Part 1 on undefined behavior in C and C++. Often I do the following in my code:
int i = 10;
i = (++i) % 7;
Does this produce undefined behavior? On x86? ARM? Perhaps it depends on the compiler?
It’s undefined behavior because
iis modified more than once without an intervening sequence point.It depends on the compiler only in the sense that there are no requirements about what the code will do, so every compiler can do something different. To be clear –
just becauseeven though you get results that seem to make sense (sometimes), the code is a bug.