-3

我写了一个 C++ 程序,实际上它是一个游戏。我收到了这个错误:

Windows 在 bla bla 中触发了断点...

有人能帮我吗?
那是代码,但在 AAA 行发生错误:

void r_motions(char **map,int size)
{
    int parameter_i,parameter_j,player_i,player_j;
    int *r_location_i = new int[1],*r_location_j = new int[1];
    player_finder(map,size,player_i,player_j);
    int r_num = robots_finder(map,size,r_location_i,r_location_j);
    for(int i=1;i<=r_num;i++)
    {
        parameter_i =0;
        parameter_j =0;
        if(r_location_i[i]>player_i) parameter_i = -1;
        if(r_location_i[i]<player_i) parameter_i =  1;
        if(r_location_j[i]>player_j) parameter_j = -1;
        if(r_location_j[i]<player_j) parameter_j =  1;
        map[r_location_i[i]][r_location_j[i]] = '.';
        r_location_i[i] = r_location_i[i]+parameter_i;
        r_location_j[i] = r_location_j[i]+parameter_j;
    }
    for(int i=1;i<=r_num;i++)
    {
        switch (map[r_location_i[i]][r_location_j[i]])
        {
        case '.': 
            map[r_location_i[i]][r_location_j[i]] = '+';
            break;
        case '@': 
            map[r_location_i[i]][r_location_j[i]] = '+';
            print_map(map,size);
            cout << "Robots win ." << endl;
            sleep(1000);
            exit(1);
            break;
        case '+':
            map[r_location_i[i]][r_location_j[i]] = '*';
            break;
        case '*': 
            map[r_location_i[i]][r_location_j[i]] = '*';
            break;
        default: cout << "what r u doin' ??";
            break;
        }
    }
}
4

1 回答 1

3

好吧,对于初学者来说,看看这个:

int *r_location_i = new int[1], ...;
...
for(int i=1;i<=r_num;i++)
  {
    parameter_i =0;
    ...
    if(r_location_i[i]>player_i) parameter_i = -1; // reading outside the array
    ...
    r_location_i[i] = r_location_i[i]+parameter_i; // writing outside the array
    ...
  }

回去研究数组。在您确切知道上面的代码有什么问题之前,不要触摸另一个指针。

于 2012-10-03T15:36:41.597 回答