Comment by xigoi
5 months ago
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?
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.
Naming an externally linked function with the prefix "str" is by itself UB since that prefix is reserved for <string.h>.
1 reply →
Note, that this is not true for C.