空指针解引用
C++-Undefined-Behavior-55
空指针未指向任何对象或函数,解引用空指针会导致未定义的行为。
示例:
int* a = nullptr;
*a = 1; // Undefined behavior
a[5] = 2; // Undefined behavior
memset(a, 0, 10); // Undefined behavior
struct T {
int i;
int f();
};
T* p = nullptr;
p->i = 0; // Undefined behavior
p->f(); // Undefined behavior
using F = void (*)();
F fp = nullptr;
fp(); // Undefined behavior
依据
ISO/IEC 14882:2003 8.3.2(4)-undefined
ISO/IEC 14882:2011 8.3.2(5)-undefined