某些运行中的段错误而不是其他运行(具有相同的输入文件)

时间:2014-09-09 03:34:08

标签: c++

我是CS学生参加c ++课程。我有一个奇怪的错误,程序有时在同一输入上运行一个seg错误(有时不会)。我对可能发生的事情感到非常困惑。 有没有人有任何想法? 我的帖子按此顺序排列:我的代码,输入文件,输出(3个不同的输出使用相同的输入)。

#include <iostream>
#include <fstream>
#include <string>

struct Class {
    std::string name;
    double totalWork;
    double totalLearned;
    bool hasVisited;
    Class* neighbors;
    Class* previousClass;
};

void searchClassOne(int indexCurrent, int indexNeighbor, int numClasses, double maxWork, 
                    double& maxLearning, Class* classes)
{
    if(indexCurrent == numClasses - 1)  return;

    if(indexNeighbor == numClasses - 2) {
        indexNeighbor = 0;
        indexCurrent++;
    }

    int totalWork = classes[indexCurrent].totalWork +         
        classes[indexCurrent].neighbors[indexNeighbor].totalWork;

    int totalLearning = classes[indexCurrent].totalLearned +     
        classes[indexCurrent].neighbors[indexNeighbor].totalLearned;

    if(totalLearning > maxLearning && totalWork < maxWork)  maxLearning = totalLearning;

    indexNeighbor++;

    searchClassOne(indexCurrent, indexNeighbor, numClasses, maxWork, maxLearning, classes);

    return;
}

int main(int argc, char* argv[])
{
    std::string fileName = argv[1];

    std::ifstream ifile(fileName);

    int numClasses = 0, maxWork = 0;

    ifile >> numClasses >> maxWork;

    std::string dummy;
    getline(ifile, dummy);

    Class* classes = new Class[numClasses];

    for(int index = 0; index < numClasses; index++) {
        classes[index].neighbors = new Class[numClasses - 1];
    }

    for(int index = 0; index < numClasses; index++) {
        classes[index].name = "";
        classes[index].totalWork = 0.0;
        classes[index].totalLearned = 0.0;
        classes[index].hasVisited = false;
    }

    int classNum = 0;

    std::cout << "CHECK 1" << std::endl;

    while(classNum < numClasses) {
        ifile >> classes[classNum].name;
        ifile >> classes[classNum].totalWork;
        ifile >> classes[classNum].totalLearned;

        std::string dummyTwo;
        getline(ifile, dummyTwo);

        classNum++;
    }

    std::cout << "CHECK 2" << std::endl;

    for(int i = 0; i < numClasses; i++) {
        std::cout << "CHECK 3 + " << i << std::endl;
        for(int j = 0; j < numClasses; j++) {
            std::cout << "CHECK 4 + " << j << std::endl;
            if(classes[i].name != classes[j].name)
                classes[i].neighbors[j] = classes[j];
        }
    }

    std::cout << "CHECK 5" << std::endl;

    double maxLearning = 0;

    searchClassOne(0, 0, numClasses, maxWork, maxLearning, classes);

    std::cout << "Maximum learning: " << maxLearning << std::endl;

    delete [] classes;
    classes = NULL;

    return 0;
}

输入文件读取:

4 3.14159
CSCI104 3.001 10.0
CSCI170 2.41       8.0
ENGR102  0.10 0.36
CSCI280    0.66 2.15

输出读数(多个):

Rafaels-MacBook-Pro-2:hw2 Rafael$ ./hw2q6 hw2q6.txt
CHECK 1
CHECK 2
CHECK 3 + 0
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 1
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 2
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 3
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 5
Maximum learning: 8
Rafaels-MacBook-Pro-2:hw2 Rafael$ ./hw2q6 hw2q6.txt
CHECK 1
CHECK 2
CHECK 3 + 0
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 1
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
Segmentation fault: 11
Rafaels-MacBook-Pro-2:hw2 Rafael$ ./hw2q6 hw2q6.txt
CHECK 1
CHECK 2
CHECK 3 + 0
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 1
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 2
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 3
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 5
Maximum learning: 8
Rafaels-MacBook-Pro-2:hw2 Rafael$ ./hw2q6 hw2q6.txt
CHECK 1
CHECK 2
CHECK 3 + 0
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
CHECK 3 + 1
CHECK 4 + 0
CHECK 4 + 1
CHECK 4 + 2
CHECK 4 + 3
Segmentation fault: 11

1 个答案:

答案 0 :(得分:0)

再看看这一行:

classes[i].neighbors[j] = classes[j];

j的最大值是多少? neighbors的大小是多少?

编辑:这就是我所说的额外索引变量:

for(int i = 0; i < numClasses; i++) {
    std::cout << "CHECK 3 + " << i << std::endl;
    int neighborIndex = 0;
    for(int j = 0; j < numClasses; j++) {
        std::cout << "CHECK 4 + " << j << std::endl;
        if(classes[i].name != classes[j].name) {
            classes[i].neighbors[neighborIndex] = classes[j];
            neighborIndex++;
        }
    }
}