【问题标题】:Segmentation Fault on Function Return C++函数返回 C++ 上的分段错误
【发布时间】:2016-10-10 22:12:43
【问题描述】:

我正在创建一个 cpu 调度程序,一旦 readInput 函数返回,我就会遇到分段错误。从我读过的内容来看,堆栈以某种方式被破坏了。

我已经确定了导致它的代码行,但我不知道为什么。

这里是流程类的相关部分:

class Process{      //Process class deals with the values of various variables the process will take on over the course of execution on a process
public:

Process(){
    pid = startTime = endTime = initPriority = priority = timeSlice = totalCpuBurst = totalCpuTime = totalIoTime = 0;
}

void setPid(int proId){pid = proId;}

void setPriority(int bonus){priority = initPriority + bonus;}

void setProcessStart(int start){startTime = start;}

void setTimeSlice(int t){timeSlice = t;}

void setInitPriority(int nice){
    initPriority = (int)(((nice + 20)/39.0)*30 + 0.5) + 105;
}

void setNumCpuBurst(int duration){numCpuBurst.push_back(duration);}

void setNumIoBurst(int duration){numIoBurst.push_back(duration);}

private:
int pid, startTime, endTime, initPriority, priority, timeSlice, totalCpuBurst, totalCpuTime, totalIoTime;
vector<int> numCpuBurst;
vector<int>numIoBurst;
};

这里是 readInput 函数。 cin 在运行程序时来自文件间接。该文件每行包含 4 个或更多整数,文件末尾以“***”结尾。目前我的测试文件中只有 3 个进程

void readInput(Process process[], int &processCount){           //read the input from the file until "***" is encountered
vector<int> values;
string inLine;
int n;
getline(cin, inLine);

while(inLine.compare("***")){
    stringstream stream(inLine);
    while(stream >> n){
        values.push_back(n);
    }

    for(int i = 0; i< values.size(); i++){
        if(i == 0){                                               //get nice value, set pid and priority
            process[processCount].setPid(processCount);
            process[processCount].setInitPriority(values[i]);
            process[processCount].setPriority(0);
        }
        else if(i == 1){                                           //arrival time
            process[processCount].setProcessStart(values[i]);
        }
        else if(i == 2){                                           //number of cpu bursts
            process[processCount].setTotalCpuBurst(values[i]);
        }
        else if(i >= 3){
            if(i % 2 == 1){                                         //CPU burst
                process[processCount].setNumCpuBurst(values[i]);
            }
            else{                                                   //IO burst
                process[processCount].setNumIoBurst(values[i]);
            }
        }
    }
values.clear();
getline(cin, inLine);
processCount++;
}

    cout << "break on return";
    return;
}

"break on return" 被打印,然后我收到分段错误

这是导致分段错误的块

        else if(i == 1){                                           //arrival time
            process[processCount].setProcessStart(values[i]);
        }

这是到目前为止的主要功能

int main() {
    vector<Process> active, expired, io, finished, cpu;
    Process startUp[4];
    int processCount = 0, runningCount = 0;
    readInput(startUp, processCount);
    cout << "\nafter read";
    return 0;
}

【问题讨论】:

  • 此问题已获得两票关闭,原因是“离题:[请求]推荐或查找书籍、工具、软件库、教程或其他非现场资源。”我很难看到这样的要求。在这里,投赞成票。

标签: c++


【解决方案1】:

您可以拥有超过 4 个 Process,但您只能分配只有 4 个 Processs 的数组

所有大于 4 的值都会崩溃

process[processCount].setProcessStart(values[i]);

尝试更改为关注

#define MAX_PROCESS  80
Process startUp[MAX_PROCESS];

MAX_PROCESS 是您的文件可以拥有的最大进程数

根据您之前的回答,似乎 4 是您的问题,但根据您的评论,我建议使用回溯更新问题。

【讨论】:

  • 该文件只有3个测试进程。我尝试了您的建议,但仍然收到段错误
  • 我建议你去像gdb这样的调试器并运行程序,一旦它崩溃运行bt命令并发布你的答案。
猜你喜欢
  • 1970-01-01
  • 2021-06-18
  • 2015-02-20
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 2019-08-13
  • 1970-01-01
  • 2015-12-28
相关资源
最近更新 更多