Yeah, completely different thread and standards being referenced. Daniel's reference C11, I'm referencing C++11, and both in very different contexts.
Conversation
Then can we agree that since C is a subset of C++, there exist some dialects and versions of C and/or C++ where while(1); is undefined behavior?
2
C is absolutely not a subset of C++. Anyone who claims such either does not know C or does not know C++, full stop.
1
3
Could you point out a construct that is valid in C99 but invalid in C++?
1
void func(int x) {
int [x] arr;
}
Will that do it for you?
For how about?
char* x = malloc(5);
1
1
The first one requires VLAs in C99, which are not supported in C++ (and never should have been supported in C, IMO), and the second requires an implicit void cast, which C++ requires the use of `reinterpret_cast` for, making the implicit cast explicit.
2
Part of the reason I stress the difference between C and C++ is their superficial similarity has been the source of many, many bugs.
1
I donโt know what to believe anymore. One half of the root twitter tree says infinite loops without side effects are UB. This chain makes persuasive arguments to the contrary.
Whatโs a decent rule of thumb regarding what to avoid in the context of loops in C/C++?
2
In C, infinite loops are well-defined. However, in the most recent C standards, it's permitted for the compiler to assume that loops terminate if they do not have a constant expression (per restrictions on those in C) as their condition and they do not have any side effects.
1
2
I think it's very clear that this was a change in semantics, but the scope of the change is very small. It also says nothing about infinite recursion. The C standard glosses over how functions are supposed to work and C implementations essentially need an infinitely large stack.
1
1
Clearly, they cannot actually have one, but the C standard does not allow undefined behavior on infinite recursion. In practice, it's going to overflow the stack, but it's not permitted to optimize it out as LLVM does in some cases due to an implementation bug. It's not allowed.
It's also not permitted to let the stack overflow go undetected and clobber other things in memory. It's not explicitly required to detect it, but any sane implementation should reliably detect it and abort safely. Clang can't do that outside Windows. It's only safe on Windows.
1
The semantics adopted in C++11 and later for optimization assumptions about infinite loops and the semantics in previous standards don't match C. I don't know the C++ standard well enough to say much about how it works there. I do know how it works in C quite well though.
1
1
Show replies


