我正在創建一個cpu調度程序,一旦readInput函數返回,我將收到分段錯誤。從我讀過的東西中,堆棧以某種方式被破壞。函數返回的分段錯誤C++
我確定了導致它的代碼行,但我找不出原因。
下面是過程類的相關部分:
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;
}
「上回破」的印刷,然後我收到分段錯誤
這是導致分割故障塊
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;
}
此問題已獲得兩張投票結束,原因是「脫離主題:[請求]推薦或找到書籍,工具,軟件庫,教程或其他非現場資源。」我很難看到這樣的要求。在這裏,有一個upvote。 –