结构异常错误
Unusual bug with structs
在这个程序中,我需要从文本文件中读取信息,第一行是我正在读取的课程数量,以处理信息,下一行"chunk"是课程名称,最后一行"chunk"列出了特定课程的先决条件。所以,
18 //number of courses to read in
CSC111 //course 1
CSC112 //course 2
etc....
2 6 //course 1 is a prereq for course 2 and 6
7 8 9 11 15 //course 2 is prereq for course 7,8,9,11,15
etc....
我已经找到了将行标记化的方法,我需要将给定课程有多少依赖项的信息放入"course"结构中,其中一个变量是"numDependencies"。我已经声明了一个N+1个课程对象的数组(索引从1开始,而不是0)。因此,课程1的numDepenedencies应该是2,课程2的numDependencies应该为5,依此类推。问题是,一旦我达到课程11,我的numDeperencies变量就会被设置为297796848这样的值,我不知道为什么。这是我的课程结构和主要
typedef struct Course
{
int numDependencies;
int numPrerequisites;
string name;
int dependencies[6];
} Course;
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include "Course.h"
using namespace std;
int main()
{
int N; //Number of courses
ifstream infile("CSCCourses.txt");
if (infile.fail()){
cout << "File not found." << endl;
//exit(0);
}
//read in number of courses
infile >> N;
//dynamically allocate array of courses
Course* courseArray = new Course[N+1];
//loop N times to read course names
for (int i = 1; i <= N; i++){
infile >> courseArray[i].name;
cout << courseArray[i].name << endl;
}
//loop again to read course information
//string str; //maybe this variable should be declared here?
for (int j = N; j <= N+12; j++)
{
cout << "Course " << j - N << endl;
string str;
getline(infile, str);
//Skip delimiters
string::size_type lastPos = str.find_first_not_of(" ", 0);
//Find first non delimiter
string::size_type pos = str.find_first_of(" ", lastPos);
while (string::npos != pos || string::npos != lastPos)
{
//Found token, put prereqs in course
string numAsStr = str.substr(lastPos, pos - lastPos);
//convert numasstr to integer
int num = atoi(numAsStr.c_str());
cout << num << endl;
//use integer to set values properly in array of courses
if(num != 0) {
courseArray[j].numDependencies++;
}
lastPos = str.find_first_not_of(" ", pos);
//find next non delimiter
pos = str.find_first_of(" ", lastPos);
}
int number = courseArray[j].numDependencies;
cout << "Number of dependencies is " << number << endl;
cout << "--------------------------" << endl;
}
infile.close();
}
在while循环之前添加下一个代码:
courseArray[j].numDependencies = 0;
编辑:
原来的答案有点错误。我忽略了你是如何分配数组的。
实际问题是下一个:您正在分配N+1个元素,并尝试访问N+2、N+3等元素。所以你要么做:
Course* courseArray = new Course[N+13];
而不是
Course* courseArray = new Course[N+1];
或者像这样更改访问元素代码(注意"-N"代码):
if(num != 0) {
courseArray[j-N].numDependencies++;
int number = courseArray[j-N].numDependencies;
您已经将数组的大小定为Course* courseArray = new Course[N+1];
,但第二个for循环是for (int j = N; j <= N+12; j++)
,它将超出数组的边界。当然,在那之后,您立即使用cout << "Course " << j - N << endl;
修复它,但后来您忘记了使用courseArray[j].numDependencies++;
和int number = courseArray[j].numDependencies;
再次修复它
但是,为什么要将j初始化为N并进行13次迭代呢?你应该做N!正如我上面所说,使用j-N应该可以解决它,但更好的解决方案是通过使第二个for循环与第一个相同来修复它,如下所示:
for (int j = 1; j <= N; j++){
当然,如果你这样做,你也必须修复你的cout,使用j
而不是j - N
。
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- C++中"覆盖功能的异常规范比基本版本更宽松"的奇怪错误
- [longjmp/setjmp]c++ 相同的代码窗口有异常 Linux 没有错误并且运行良好
- 发生此错误:在0x0F2BFB7C(基于.dll)时引发异常 尝试更改大小是问题吗?
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 引发异常:读取访问冲突.**此**0x40错误
- 从 C# 调用 C++ DLib 会导致错误的分配异常
- 错误 : 异常 : 引发未处理的异常: 读取访问冲突.0xDDDDDDDD临时温度
- Windows错误:异常:使用从C++到Python的ctypes创建DLL时出现访问冲突或Windows错误193
- C++执行期间的类成员函数错误/异常处理
- 无法捕获C++长度错误异常
- 内存读/写错误异常
- 字符串转换错误:异常还是错误代码
- 类中的错误/异常捕获
- 在库中引发逻辑错误异常或中止
- POCO C++SAX解析器:如果xml文档编码是ANSI,那么下一条语句不是读取并抛出编码错误异常
- 捕获 Segfault 或任何其他错误/异常/信号C++例如在 Java 中捕获异常