运行时检查失败 #2 - 变量"数字选择"周围的堆栈已损坏
Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted
每次完成调试程序时,我都会遇到此终端错误。
我在做什么:
[这个程序是一个简单的彩票号码比较用户输入号码与非重复的随机彩票号码,例如,如果它得到 4 个 6 的权利怎么办]
但事实证明,该程序不起作用或至少不稳定。
这是我的代码:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <time.h>
#include <ctime>
#include <algorithm>
using namespace std;
int main()
{
cout << "[La Loteria Electronica]n";
cout << "Escoge 6 n" << char(163) << "meros del (1 al 49): n";
int numberchoices[] = { 0 };
for (int w = 1; w < 7; w++)
{
cout << "N" << char(163) << "mero #" << w << ": ";
cin >> numberchoices[w];
} // user numbers
//lottery numbers
int i, j, k, nums[51];
srand((int)time(0));
for (i = 1; i < 50; i++) nums[i] = i;
for (i = 1; i < 50; i++)
{
j = (rand() % 49) + 1;
k = nums[i]; nums[i] = nums[j]; nums[j] = k;
}
cout << "The lottery numbers are: ";
for (i = 1; i < 7; i++) cout << nums[i] << " ";
if (numberchoices[i] = nums[i])
{
cout << "gudn";
}
if (numberchoices == nums)
{
cout << "gud 2";
}
/**/
cout << "nn";
system("pause");
请 ?
int numberchoices[] = { 0 };
for (int w = 1; w < 7; w++)
{
cout << "N" << char(163) << "mero #" << w << ": ";
cin >> numberchoices[w];
} // user numbers
您正在声明一个大小为 1 的数组,然后将其使用到位置 6 ?
每次完成调试程序时,我都会遇到此终端错误。
我很惊讶每次开始调试时都没有出现终端错误。
从 1 到 6 的位置numberchoises
的访问是 UB(未定义的行为)。也就是说:一切都可能发生。
解决方案:尝试使用
int numberchoices[7] = { }; // initialize all elements to zero!
另一点
if (numberchoices == nums)
不确定你得到了你所期望的。
是否要将对应于numberchoices
的整数指针(int[1]
建议int[7]
)的整数指针与对应于nums
(int[51]
)的整数指针进行比较?
相关文章:
- 如何使用默认参数等选择模板专业化
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 选择要调用的构造函数
- C++选择排序算法中的逻辑错误
- QTreeView幻灯片多选后无法使用单击选择
- 无法获取菜单选择以运行函数.C++
- Qt C++静态thread_local QNetworkAccessManager是线程应用程序的好选择吗
- 在C++中,如何通过几种类型从元组中选择多个元素
- 讨论 - 创建矩阵时的数组与向量的向量 - 什么是最实用的选择
- 对可变参数使用声明.如何选择正确的功能
- 选择选举获胜者的程序
- 如何选择在 csv 文件中输出的位置
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 选择和修改嵌套向量中的条目的最佳实践
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 在运行时选择父类的实现
- 运行时检查失败 #2 - 变量"数字选择"周围的堆栈已损坏
- 如何禁用ListView选择视觉效果并绘制项目周围的矩形