好的,所以我正在教我的女朋友一些 C++,她写了一个我认为行不通的程序,但它确实有效。它访问数组中的另一个元素(例如,访问数组 [5] 以获取大小为 5 的数组)。这是缓冲区溢出的实例吗?我的想法是它直接在数组之后写入/访问内存,这是正确的吗?基本上我的问题是..为什么这行得通?
#include <iostream>
using namespace std;
int main()
{
int size;
cout << "Please enter a size for the array." << endl;
cin >> size;
cout << endl;
cout << "There are " << size << " elements in this array." << endl;
cout << endl;
cout << endl;
cout << endl;
int array[size];
for (int counter = 1; counter <= size; counter++)
{
cout << "Please enter a value for element " << counter << "." << endl;
cin >> array[counter];
}
cout << endl;
cout << endl;
for (int counter = 1; counter <= size; counter++)
{
cout << "Element " << counter << " is " << array[counter] << "." << endl;
cout << endl;
}
cout << "*bing! :)" << endl;
cout << endl;
return 0;
}