在C++或C中,如果将指针设置为超出内存边界的值,会发生什么?下面是可能导致这种情况发生的代码:
int* ptr = 0;
while (true) {
ptr += 1; // eventually this will go out of bounds of the memory ... unless there is an overflow.
*ptr = 10; // just give it a value, why not?
}
发生了什么?当指针超出16字节内存的范围时,它会从0xF翻转到0x0吗?它是否继续计数并且*ptr = 10;
这一行会导致计算机崩溃?
我不至于蠢到去尝试,但我非常好奇。