At what point in the loop does integer overflow become undefined behavior?

后端 未结 12 2243
南方客
南方客 2020-12-07 18:12

This is an example to illustrate my question which involves some much more complicated code that I can\'t post here.

#include 
int main()
{
           


        
12条回答
  •  死守一世寂寞
    2020-12-07 19:01

    If you're interested in a purely theoretical answer, the C++ standard allows undefined behaviour to "time travel":

    [intro.execution]/5: A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation)

    As such, if your program contains undefined behaviour, then the behaviour of your whole program is undefined.

提交回复
热议问题