Skip to content

Infinite Loop

Is infinite loop undefined behavior?

I saw this interesting meme in QQ Zone:

meme

I tested in Godbolt and got exactly the same result:

result

At the first glance, I thought this would be a bug for compiler since optimizing away a infinite loop is somewhat unacceptable and I usually use while(1); to construct TLE in OnlineJudge.

However, according to standard Basics.Program execution.Multi-threaded executions and data races.Forward progress, it seems to be an UB.

standard

"the compiler may assume X about the program" is logically equivalent to "if the program doesn't satisfy X, the behaviour is undefined", so we should believe that an infinite loop without doing anything at all is an UB.

Here explains why this is UB: N1528

Comments