简体   繁体   English

在某些运行中发生段故障,而不在其他运行中发生段故障(具有相同的输入文件)

[英]Seg-fault on some runs and not on others (with the same input file)

I'm a CS student taking a c++ class. 我是一名上c ++课的CS学生。 I have an odd error, where the program sometimes runs a seg fault on the same input (and sometimes not). 我有一个奇怪的错误,该程序有时会在同一输入上运行seg错误(有时不会)。 I'm very confused about what may be happening. 我对可能发生的事情感到非常困惑。 Does anyone have any ideas? 有人有什么想法吗? My post is in this order: my code, the input file, the output (3 different outputs using the same input). 我的帖子按此顺序排列:我的代码,输入文件,输出(使用相同输入的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;
}

Input file reads: 输入文件读取:

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

Output reads (multiple): 输出读取(多个):

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

Have another look at this line: 再看一下这一行:

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

What is the maximum value of j ? j的最大值是多少? What is the size of neighbors ? neighbors的大小是多少?

Edit: This is what I meant by an extra index variable: 编辑:这是我的意思是一个额外的索引变量:

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++;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BST的段故障remove()函数 - Seg-Fault at BST remove() Function 导致段错误的值传递变量的地址 - Address of pass-by-value variable causing seg-fault 如果不执行协程,则在 pthread seg-fault 之间共享 Lua 状态 - Shared Lua state between pthread seg-fault if not executing coroutine 尝试将context()交换到存储在队列中的结构成员时出现段错误 - Seg-fault when trying to swapcontext() into a struct member that is stored in a queue 为什么一个二维数组会导致段错误,而另一个不会? - Why one 2-d array cause seg-fault, and another do not? C / C ++程序可以通过读取数组末尾(UNIX)来解决错误吗? - Can a C/C++ program seg-fault from reading past the end of an array (UNIX)? 为什么大型的静态数组会产生段错误,而动态的却不会? (C ++) - Why does a large static array give a seg-fault but dynamic doesn't? (C++) 为什么在我的自定义 hash 表中插入大整数时出现段错误? - Why am I getting a seg-fault when inserting large integers into my custom hash table? 在不同的C ++编译器中有通用解决方案可以捕获被零除的异常类型,seg-fault? - There are universal solutions in different C++-compilers to catch the exception type divide by zero, seg-fault? 从dll /共享库返回指针会导致seg-fault - Returning a pointer back from a dll/shared-library causes seg-fault
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM