阵列周围的堆栈已损坏C++
C++ stack surrounding array is corrupted
我试图阅读一些关于变量周围的堆栈已损坏的不同帖子,但在将答案连接到我的代码时遇到问题。我想知道为什么我总是收到一条错误消息,说 scoreArray 周围的堆栈已损坏。我尝试使用多个变量来增加数组上的位置。我知道代码写得不好,但如果有人能帮助我理解为什么我会收到这条消息,我将不胜感激。
非常感谢。
double sum = 0.0;
double SumFunction(double printArray[]);
int i = 0;
int j = 0;
const int SIZE = 4;
void main()
{
double input = 0.0;
vector<double> scores;
double scoreArray[SIZE];
do
{
cout << "Please enter a decimal value: ";
cin >> input;
scores.push_back(input);
} while (scores.size() <= SIZE);
do
{
int z = 0;
scoreArray[i] = scores[z];
i++;
z++;
} while (i <= SIZE);
SumFunction(scoreArray);
cout << sum;
system("PAUSE");
}
double SumFunction(double printArray[])
{
do
{
sum += printArray[j];
j++;
} while (j <= SIZE);
return sum;
}
因为:
/*(1)*/ } while (scores.size() <= SIZE);
/*(2)*/ } while (i <= SIZE);
/*(3)*/ } while (j <= SIZE);
应该是:
/*(1)*/ } while (scores.size() < SIZE);
/*(2)*/ } while (i < SIZE);
/*(3)*/ } while (j < SIZE);
因为当索引已经超过分配的scoreArray
空间时,您的do...while
循环会进入另一个迭代。
相关文章:
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 为什么C中的通用链表中存储的数据已损坏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 运行时检查失败 #2 - 变量"e"周围的堆栈已损坏。发生
- 变量周围的堆栈'sortArray'已损坏
- 变量周围的堆栈'folderPath'已损坏
- 在为 iOS 构建 aws-sdk-cpp 时,cmake 上的 CXX 编译器错误已损坏
- 运行时检查失败 #2 - 变量周围的堆栈'...'已损坏
- AES-128 CFB-8解密的前16个字节已损坏
- 运行时检查失败 #2 - 变量"l1"周围的堆栈已损坏
- C++:此代码可以编译,但引发运行时检查失败 #2 - 围绕变量周围的堆栈'num'已损坏。发生
- 图像在本地主机上显示已损坏
- 检查工作正常的 CXX 编译器:/cygdrive/c/cygwin64/bin/clang++ -- 已损坏
- 数组变量周围的堆栈已损坏
- 运行时检查失败 - 变量周围的堆栈已损坏
- 串行端口的传入值有时已损坏或丢失
- 自定义异常中的消息已损坏