gpt4 book ai didi

c++ - std::list 用法的段错误

转载 作者:行者123 更新时间:2023-11-28 03:21:32 24 4
gpt4 key购买 nike

我是从 Java 用户转为使用 C++ 的,我很难理解这条语句出了什么问题。我的程序在我放置 push_back 命令的任何地方都出现了段错误。所以我想知道到底发生了什么。

class Process {
public:
int nice;
int arrivalTime;
int cpuBursts;
list<int> burstList;

Process() {
burstList.push_back(10); // Segfaults here...
}
};

完整代码如下:

#include<iostream>
#include<stdlib.h>
#include<fstream>
#include<list>
#include<string.h>

using namespace std;

int calcTimeslice(int priority);
int calcOriginalPrio(int nice);
int readFile(int ,char **);
int calcPrioBonus(int,int);
void tokenizeAndAdd(char *);

class Bursts {
public:
int isCPUBurst;
int time;

Bursts() {}
// Constructor to make it easier to add to list
Bursts(int tempIsCPU, int tempTime) {
isCPUBurst = tempIsCPU;
time = tempTime;
}

};

class Process {
public:
int nice;
int arrivalTime;
int cpuBursts;
list<int> burstList;

Process() {
burstList.push_back(10);
}
};



int main(int arg, char **argv) {

// This is if the file was not correctly read into the program
// or it doesnt exist ...
if(readFile(arg,argv)==-1) {
cout << "File could not be read. \n";
return -1;
}
//cout << "Original Calc Whatever: " << calcOriginal(19) << '\n';
return 0;

}

/*
* Calculates the timeslice based on the priority
*/
int calcTimeslice(int priority) {
double finalCalc;

// This is the given function in the prompt
finalCalc = ( (1 - (priority / 140)) * 290 + (.5) ) + 10;

// Cast to int, this will be a truncate
return ((int)finalCalc);
}

int readFile(int arg, char **argv) {
char *temp,*pointer;
int endOfFile = 1;

// While its not the end of the file
while(endOfFile) {
// Read in the input from stdin
fgets(temp,256,stdin);

// Check to see if this line had a * in it
if(*temp =='*')
endOfFile = 0;
else
tokenizeAndAdd(temp);
}

return 0;

}

void tokenizeAndAdd(char *string) {
char *token = strtok(string," \n");
int i = 0;
Process p;

while(token != NULL) {
cout << token << endl;
if(i>2) { // If it is odd (CPU burst)
if(i%2 == 1) {
int tempInt = atoi(token);
//p.burstList.push_back(tempInt);
}
else { // If it is even (IO burst)
int tempInt = atoi(token);
//p.burstLis.push_back(tempInt);
}
}
else if(i==0)
p.nice = atoi(token);
else if(i==1)
p.arrivalTime = atoi(token);
else if(i==2)
p.cpuBursts = atoi(token);

token = strtok(NULL," \n");
i++;
}

//cout << p.nice << " " << p.arrivalTime << " " << p.cpuBursts << "\n";
//i = 0;
//cout << p.burstList.size() << "\n";
// cout <<
//}
return;
}

/*
* Calculates and returns the original priority based on the nice number
* provided in the file.
*/
int calcOriginalPrio(int nice) {
double finalCalc;

// This is the given function from the prompt
finalCalc = (( nice + 20 ) / 39 ) * 30 + 105.5;

// Cast to int, this is a truncate in C++
return ((int)finalCalc);
}

/*
* Calculates the bonus time given to a process
*/
int calcPrioBonus(int totalCPU, int totalIO) {
double finalCalc;

// How to calculate bonus off of the prompt
if(totalCPU < totalIO)
finalCalc = ( (1 - (totalCPU / (double)totalIO)) * (-5)) - .5;
else
finalCalc = ( (1 - (totalIO / (double)totalCPU)) * 5) + .5;

// Cast to int
return ((int)finalCalc);
}

最佳答案

您正在使用以下代码中未初始化的 temp:

char *temp;
...
while(endOfFile) {
fgets(temp,256,stdin);
...

这可能会产生任何副作用,因为它很可能会破坏您的堆栈或部分堆内存。它可能会立即失败(当调用 fgets() 函数时),它可能会稍后失败(如在您的示例中)或者它甚至可能运行良好 - 可能直到您升级操作系统、编译器或其他任何东西,或者直到您想在另一台机器上运行相同的可执行文件。这称为未定义行为

您需要为 temp 变量分配空间,而不仅仅是一个指针。使用类似的东西

char temp[256];
...
while(endOfFile) {
fgets(temp,256,stdin);
...

有关详细信息,请参阅 fgets()文档。第一个参数是指向 char 数组的指针 - fgets() 将存储已读取的字节。在您的代码中,您传递了一个未初始化的指针,这意味着 fgets() 会将字节存储到未定义的内存位置 - 这会被操作系统捕获,并以段错误终止您的应用程序。


顺便说一句:你应该考虑在编译时启用迂腐的警告——我用

编译
g++ -Wall -pedantic -o list list.cpp

这给了我以下警告:

list.cpp: In function 'int readFile(int, char**)':
list.cpp:76:26: warning: 'temp' may be used uninitialized in this function [-Wuninitialized]

关于c++ - std::list 用法的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com