g++ optimization breaks for loops

前端 未结 3 1195
不知归路
不知归路 2021-01-12 07:52

A few days ago, I encountered what I believe to be a bug in g++ 5.3 concerning the nesting of for loops at higher -OX optimization levels. (Been experiencing it

3条回答
  •  一整个雨季
    2021-01-12 08:09

    Your code invokes undefined behaviour, since the int sum overflows. You say "this shouldn't in any way affect the other variables". Wrong. Once you have undefined behaviour, all odds are off. Anything can happen.

    gcc is (in)famous for optimisations that assume there is no undefined behaviour and do let's say interesting things if undefined behaviour happens.

    Solution: Don't do it.

提交回复
热议问题