下面的代码是undefined behavior in C++ (尽管它在几乎所有广泛使用的实现上都可以正常工作):
int* pointer; //uninitialized - likely illegal pointer value
pointer++; //incrementing an illegal pointer is UB
上面的代码在C中合法吗?
最佳答案
这在 C 中也是未定义的行为,因为在某些体系结构上,将无效指针加载到寄存器会触发硬件故障。
参见 Is storing an invalid pointer automatically undefined behavior?
关于c++ - 在 C 中使用无效指针值是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4076563/