Comment by account42

5 months ago

Infinite loop without side effects == program stuck and not responding on user input and not outputting anything. That's not something a useful program will ever want to do.

Not true, C++ made it so trivial infinite loops are not UB because it turns out they do have legitimate uses.

https://lists.isocpp.org/std-proposals/2020/05/1322.php

https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p28...

  • Yes, the C++ committee has been making some stupid decisions lately. This is not the only one.

    Low level platform-specific code that needs to hot spin until an interrupt happens can use assembly for that part which it will need to do for the interrupt handler anyway.

    • You don't even need to use assembly for this, the wait for interrupt typically involves side effects.

The problem is when you accidentally write an infinite loop. In a different language, you run the code, see that it gets stuck and fix it. In C, the compiler may delete the function, making it hard to realize what is happening.

  • This is not a problem that C or C++ programmers actually encounter, ever.

    • I actually encountered it a couple weeks ago.

      Can you spot the infinite loop in this function?

        char* strcpy(char* restrict d, const char* restrict s) {
          stpcpy(d, s);
          return d;
        }
      

      I'll help. A call to `stpcpy` that ignores the return value can be swapped with a call to the (more likely to be optimized) `strcpy`. Since that's infinite recursion, and there is no forward progress, it's undefined behavior and anything goes.

      This isn't just theory, it actually broke things in practice for me.

      2 replies →