听说最近大家在讨论雷,我目前觉得最雷的应该就是这个了:
int main() {
while (true); // UB in C++11 or later
}
是的,infinite loop 在 C++11 是 undefined behavior:
4.7.2 [intro.progress]
The implementation may assume that any thread will eventually do
one of the following:
— terminate,
— make a call to a library I/O function,
— perform an access through a volatile glvalue, or
— perform a synchronization operation or an atomic operation.
( 注:当标准说你可以 assume P 的时候,言下之意就是 not P 是 UB )
很明显上面的 loop 不属于这四个的其中一个
==============================================================================
于是国外就有无聊人士(?)造了一个例子用穷举法找费式最后定理的反例,
理论上当然是找不到的,所以该 loop 应该是个无穷循环:
#include <cstdint>
#include <iostream>
bool fermat() {
constexpr int32_t MAX = 1000;
int32_t a = 1, b = 1, c = 1;
// Infinite loop
while (true) {
if (((a*a*a) == ((b*b*b)+(c*c*c))))
return false;
++a;
if (a > MAX) { a=1; ++b; }
if (b > MAX) { b=1; ++c; }
if (c > MAX) { c=1; }
}
return true;
}
int main() {
if (!fermat())
std::cout << "Fermat's Last Theorem has been disproved.";
else
std::cout << "Fermat's Last Theorem has not been disproved.";
std::cout << std::endl;
}
$ clang++ -O2 test.cpp && ./a.out
Fermat's Last Theorem has been disproved.
Oops.
( 如果用 -O0 或是 GCC 的确是会进入无穷循环 )
==============================================================================
那在 C 底下的行为是怎么样呢?C11 的标准如此说:
6.8.5 Iteration statements
6 An iteration statement whose controlling expression is not a constant
expression (156) that performs no input/output operations, does not access
volatile objects, and performs no synchronization or atomic operations
in its body, controlling expression, or (in the case of for statement) its
expression-3, may be assumed by the implementation to terminate. (157)
157) This is intended to allow compiler transformations such as removal of
empty loops even when termination cannot be proven
while(1); 的 1 刚好是一个 constant expression ,所以这条不适用,
但是稍微修改一下变成 while(1,1); 多了 comma op 就不是 constant expression 了,
这样的话 compiler 的确是可以把 while(1,1); 拿掉的
==============================================================================
同场加映,踩到 UB 的下场:
#include <stdio.h>
static void (*fp)(void);
void kerker(void) {
puts("rm -rf /");
}
void never_called(void) {
fp = kerker;
}
int main(void) {
fp();
return 0;
}
$ clang test.c -O2 && ./a.out
rm -rf /