C++ 变量“ ”周围的堆栈已损坏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5135900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:29:17  来源:igfitidea点击:

Stack around the variable ' ' was corrupted

c++

提问by bluetickk

void GameBoard::enterShips()
{
    char location[1];
    int ships = 0;
    int count = 1;

    while(ships < NUM_SHIPS)
    {
        cout << "Enter a location for Ship " << count << ": ";
        cin >> location;
        cout << endl;

        Grid[location[0]][location[1]] = SHIP;
        ships++;
        count++;
    }
}

Im writing a battleship game. I have the board layouts working and the computers randomly generated ships. Now I am working on this method to prompt the user to enter coordinates for the ships When I run the program, it allows me to enter 5 ships. When I enter the 6th ship, it gives me this error.

我正在写一个战舰游戏。我有电路板布局工作和计算机随机生成的船只。现在我正在研究这个方法来提示用户输入船只的坐标 当我运行程序时,它允许我输入 5 艘船。当我进入第 6 艘船时,它给了我这个错误。

Stack around the variable location was corrupted.

变量位置周围的堆栈已损坏。

Ive looked for answers online and have not found anything exclusive.

我在网上寻找答案,并没有找到任何独家的东西。

Any help would be appreciated.

任何帮助,将不胜感激。

回答by SLaks

locationis an array of a single char.
There is no location[1].

location是单个char.
没有location[1]

回答by Tugrul Ates

You are prompting the memory address of locationarray to your user. You should ask location indices separately:

您正在location向用户提示数组的内存地址。您应该单独询问位置索引:

void GameBoard::enterShips()
{
    int location[2];
    int ships = 0;
    int count = 1;

    while(ships < NUM_SHIPS)
    {
        cout << "Enter a location for Ship " << count << ": ";
        cin >> location[0];
        cin >> location[1];
        cout << endl;

        Grid[location[0]][location[1]] = SHIP;
        ships++;
        count++;
    }
}

Notice int location[2];since an array of size 1 can only hold one element. I also changed the element type to int. Reading char's from the console will result in ASCII values, which are probably not what you want.

请注意,int location[2];因为大小为 1 的数组只能容纳一个元素。我还将元素类型更改为 int。从控制台读取字符将产生 ASCII 值,这可能不是您想要的。

回答by James McNellis

cin >> location;
cin >> location;

locationis an array of one char. This can't succeed because when you read from a stream into a chararray, a null terminator has to be added (which takes one character). You will inevitably overrun the bounds of the array.

location是一个数组char。这不会成功,因为当您从流读取到char数组时,必须添加一个空终止符(它需要一个字符)。您将不可避免地超出数组的边界。

You can use a std::string, which will help you avoid any buffer overrun issues:

您可以使用std::string,这将帮助您避免任何缓冲区溢出问题:

std::string location;
if (!(std::cin >> location)) {
    // handle input error
}

Note also that you probably need to convert the string representations of the numbers into numeric values. You can easily do this by reading from the stream into two intobjects instead:

另请注意,您可能需要将数字的字符串表示形式转换为数值。您可以通过从流中读取到两个int对象来轻松完成此操作:

int x_location, y_location;
if (!(std::cin >> x_location >> y_location)) { 
    // Handle input error
}

if (x_location >= X_DIMENSION || x_location < 0 ||
    y_location >= Y_DIMENSION || y_location < 0) {
    // Handle out-of-range error 
}

// use x_location and y_location

回答by SoapBox

You made the locationvariable only able to hold a single character. You access it expecting it to hold at least 2 characters. If you're using cin and expecting to read exactly two characters, a better method would be:

您使location变量只能容纳一个字符。您访问它时期望它至少包含 2 个字符。如果您正在使用 cin 并希望读取两个字符,则更好的方法是:

char locationX, locationY;
// ...
std::cin >> locationX >> locationY;
// ...
Grid[locationX][locationY] = SHIP;