#include <iostream>
int main()
{
int array1[10] = {0};
char* array2[10] = {'\0'};
for (int i = 0; i <= 100; i++)
{
std::cout << array1[i]; // This does not crash
//std::cout << array2[i]; // This crashes
array1[i]; // Wont crash here
array2[i]; // nor here, Why? because there is no cout??
}
return 0;
}
好的,对于在这里回答的人,我知道我故意让数组溢出。那么,为什么程序在“cout”时崩溃,而在其他情况下却没有?
谢谢!
最佳答案
鉴于该值未被使用,它可能被编译器优化掉并且在运行时从未真正检索到。
关于溢出时的 C++ 数组绑定(bind)异常 - "cout"并且没有 "cout",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4482784/