Dan*_*ens 5 c++ undefined-behavior
我仍然无法准确指出实际定义了未定义行为的级别。假设以下代码:
int i = value;
unsigned int x = static_cast<unsigned int>(i);
Run Code Online (Sandbox Code Playgroud)
这是有效的 C++ 代码并定义为 egi是1. 但是i = -1它变成了未定义的行为,因此应用程序在运行时处于 UB 状态。
在下面的代码示例中,UB 在编译时已经很明显了。所以我的问题是,UB 可以在编译时或运行时出现是否正确?这里的正确术语是什么?
void foo(int* p)
{
int v = *p;
if (p == nullptr)
return;
}
Run Code Online (Sandbox Code Playgroud)