3 ms·
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 ma
by xigoi 5mo 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.
- account42 5mo agoThis is not a problem that C or C++ programmers actually encounter, ever.
- ncruces 5mo agoI 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.
- rssoconnor 5mo agoNaming an externally linked function with the prefix "str" is by itself UB since that prefix is reserved for <string.h>.
- ncruces 5mo agoRight. I am doing a standard library replacement, and forgot I needed to compile freestanding. Oops. My bad. So `str` and `mem` are reserved. But then so are `to` and `is` (by <ctype.h>). Just forget about having a function named `is_valid_user`. And so are `mtx_`, `cnd_`, `thrd_`, `atomic_`, `memory_`... Which is why... everything in C is UB.
- 1718627440 5mo agoNote, that this is not true for C.