【问题标题】:Error when accessing array - stack around the variable 'scores' was corrupted [closed]访问数组时出错-变量'scores'周围的堆栈已损坏[关闭]
【发布时间】:2014-06-23 16:10:21
【问题描述】:

我是 C++ 新手,编写了一些代码,但出现以下错误:

运行时检查失败 #2 - 变量“scores”周围的堆栈已损坏

是什么导致了这个错误?

这是我的代码:

#include <iostream> // Enables cout and endl
#include <string>
#include <sstream>
#include "stdafx.h"

using namespace std;
int getInput();

int main()
{
    int scores[5];
    int i;
    int j;
    int numberOfScores;

    for (i = 0; i < 6; i++) // Sets all 5 elements of the array to zero
    {
        scores[i] = 0;
    }

    cout << "How many scores do you have to enter?\n" << endl;
    cin >> numberOfScores;


    for (j = 0; j < numberOfScores; j++) // Gather test scores and increases each array index as that score is entered
    {
        scores[getInput()] ++;
    }

    cout << "The number of zeros: " << scores[0] << endl;
    cout << "The number of ones: " << scores[1] << endl;
    cout << "The number of twos: " << scores[2] << endl;
    cout << "The number of threes: " << scores[3] << endl;
    cout << "The number of fours: " << scores[4] << endl;
    cout << "The number of fives: " << scores[5] << endl;


    return 0;
}

int getInput()
{
    int enteredScore;
    cout << "Enter the test scores one at a time.\n";
    cout << "The range of scores is 0 to 5.\n";
    cin >> enteredScore;

    if (enteredScore >= 0 && enteredScore <= 5)
    {
        return enteredScore;
    }
    else
    {
        cout << "Error!  The range of scores is 0 to 5.\n";
        cout << "Enter the test scores one at a time.\n";
        cin >> enteredScore;
        return enteredScore;
    }
}

【问题讨论】:

  • for (i = 0; i &lt; 6; i++) 应该是for (i = 0; i &lt; 5; i++),否则它将访问分数[5],由于基于 0 的索引(0、1、2、3、4 是有效的,它被关闭了 1,这就是您请求存储的 5 个元素)。
  • 请先调试后再问!

标签: c++ arrays indexoutofboundsexception


【解决方案1】:

你有一个错误

 cout << "The number of fives: " << scores[5] << endl;

您的数组大小为 5,但您正在访问第 6 个元素。

for (i = 0; i &lt; 6; i++) 相同应该是i &lt; 5

【讨论】:

  • 他实际上正在访问索引 5
【解决方案2】:

看来这个声明:

int scores[5];

不正确。这将创建一个包含 5 个数字的数组,索引来自 scores[0-4],但是,您会不断地引用 score[5],这是整个程序中数组的 第六个 元素。建议改成

int scores[6];

【讨论】:

  • 谢谢!很抱歉没有先调试 - 老实说,我还不知道该怎么做。我试过(在 Visual Studio 中),但什么也没发生。对于那个很抱歉。无论如何,int score[6] 完全符合我的要求。
  • @sparky 注意,这本身不足以解决问题,因为您也没有正确验证用户输入。
【解决方案3】:

问题:

您在多个地方越界访问您的数组。

在这里,当您只有 5 个元素时,您循环遍历 6 个元素:

for (i = 0; i < 6; i++) // Loops through 6 elements
    {
        scores[i] = 0;
    }

这里调用getInput()并使用返回值作为索引:

scores[getInput()] ++;

但是,函数的前半部分接受用户的输入,范围为 0 到 5,因此允许访问 6 个元素:

if (enteredScore >= 0 && enteredScore <= 5)

如果用户输入超出该范围的数字,情况会变得更糟,因为他们随后有第二次机会输入数字,只是这一次没有验证并且他们输入的任何数字都被接受:

cin >> enteredScore;
return enteredScore;

最后,您再次尝试在此处访问第 6 个元素:

cout << "The number of fives: " << scores[5] << endl;

解决方案:

首先,您需要做以下两件事之一:

  • 更改for 循环、if 语句和cout 语句,使其不访问索引5

或:

  • 创建数组,使其包含 6 个元素:int scores[6];

其次,您需要修复 getInput() 函数中的错误,以便正确验证输入。例如,你可以试试这个:

int getInput()
{
    int enteredScore;
    cout << "Enter the test scores one at a time.\n";
    cout << "The range of scores is 0 to 4.\n";
    cin >> enteredScore;

    while (enteredScore < 0 || enteredScore > 4)
    {
        cout << "Error!  The range of scores is 0 to 4.\n";
        cout << "Enter the test scores one at a time.\n";
        cin >> enteredScore;
    }
    return enteredScore;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多