变量周围的调试错误堆栈已损坏

Debug Error Stack Around Variable Was Corrupted

本文关键字:堆栈 已损坏 错误 调试 周围 变量      更新时间:2023-10-16

我是一名编程爱好者,遇到过一个错误,上面写着"运行时检查失败#2-变量"存储"周围的堆栈已损坏。"。

我的代码完全符合我的要求,但我不明白为什么会出现这个错误,如果有人能向我解释,我将不胜感激。谢谢!

#include <iostream>
using namespace std;
int main()
{
   int store[4] = {};
   for (int i = 0; i != 5; i++)
   {
      cout << "Enter the sales of store " << (i + 1) << ": ";
      cin >> store[i];
   }
   cout << "nSALES BAR GRAPHn(Each * represents $100)n";
   for (int i = 0; i != 5; i++)
   {
      int a = (store[i] / 100);
      cout << "nStore " << (i + 1) << ": ";
         for (int i = 0; i < a; i++)
         {
            cout << "*";
         }
   }
   cout << "n";
   system("pause");
}

您的for循环不正确:

for (int i = 0; i != 5; i++)

这引用了store[4],如果您声明了int store [4],则它是越界的。如果你想将商店大小保持为4,你应该将循环更改为:

for (int i = 0; i < 4; i++)