使用变量num时,变量堆栈会被破坏

Jun*_*dhi -2 c++

当使用变量num时,我在访问第9个索引时遇到堆栈损坏的错误,但是当我使用任何其他名称访问时,它会出错.请解释我这种含糊不清的地方.

#include<iostream>
#include<iomanip>
using namespace std;
const int SIZE = 9;

class A
{
private:

    int num[SIZE];
    int num_2[SIZE];
public:
A()
{
    for(int i = 1 ; i <= 9 ; i++)
    {
        num_2[i] = i;
    }  
}
};
void main()
{
    A obj;
}
Run Code Online (Sandbox Code Playgroud)

Cor*_*mer 6

如果SIZE = 9那么你的循环应该是

for(int i = 0; i < SIZE ; i++)
Run Code Online (Sandbox Code Playgroud)

C++使用基于0的索引,因此数组的第一个索引是[0]最后一个索引[SIZE-1]