分段错误:使用向量时的 11 c++
Segmentation fault: 11 c++ when using vector
我正在尝试为我的大学项目编写一个程序,该程序应该执行 先到先得 日程安排 我对这个功能考虑了很多,但我没有知道如何让它工作,我总是得到 Segmentation fault: 11,我也尝试使用 temp.at(j) 但它给了我 Segmentation fault: 6,我试图最小化向量,这样它就可以通过在函数外部声明向量来进入边界,然后使用 temp.size() 而不是 Processes 但它也没有用。
void FCFS(Process ProcessDetails[], int Processes)
{
vector<int> temp;
vector<int> temp1;
int first = 0; //first value to compare with.
for(int j = 0; j < Processes; j++){ // to make sure that it passes through all elements.
for(int i = 0; i < Processes; i++){ // pass each PID and Burst time to vector temp and temp1.
if(ProcessDetails[i].ArrivalTime == first){
temp.operator[](j) = ProcessDetails[i].PID;
temp1.operator[](j) = ProcessDetails[i].BurstTime;
}
}
first++;// increase first value to declare the round is finished and start a new one.
}
for(int i = 0; i < Processes; i++){ // pass the sorted vector values back to the arrays.
ProcessDetails[i].PID = temp.operator[](i);
ProcessDetails[i].BurstTime = temp1.operator[](i);
}
}
程序运行正常,直到达到这个功能,请帮助。
你的向量没有元素。
因此,使用矢量运算符[] 将失败。
使用 push_back、放置、调整大小或 some other function 向矢量添加元素。
您必须将矢量分配更改为
if(ProcessDetails[i].ArrivalTime == first){
temp.push_back(ProcessDetails[i].PID);
temp1.push_back(ProcessDetails[i].BurstTime);
}
如果向量用于访问不存在的元素,则向量 operator[]()
的行为未定义。
由于您使用了默认构造的向量,它们的大小为零 - 因此它们没有可访问的元素。
如果你使用.at()
成员函数,它会检查索引并在索引时抛出一个异常(类型std::out_of_range
,在标准头文件<stdexcept>
中声明)无效。您可以通过将代码包装在适当的 try
/catch
块中来确认这一点。
要消除该问题,您需要在使用 operator[]()
之前重新调整向量大小(例如,使用 push_back()
向其添加元素,使用 resize()
调整大小等)。并确保索引有效,因为 operator[]()
不会调整 std::vector
.
的大小
此外,temp[j]
等同于 temp.operator[](j)
。对于提供 operator[]()
函数的类型,编译器会将 temp[j]
等表达式转换为 temp.operator[](j)
.
的调用
我正在尝试为我的大学项目编写一个程序,该程序应该执行 先到先得 日程安排 我对这个功能考虑了很多,但我没有知道如何让它工作,我总是得到 Segmentation fault: 11,我也尝试使用 temp.at(j) 但它给了我 Segmentation fault: 6,我试图最小化向量,这样它就可以通过在函数外部声明向量来进入边界,然后使用 temp.size() 而不是 Processes 但它也没有用。
void FCFS(Process ProcessDetails[], int Processes)
{
vector<int> temp;
vector<int> temp1;
int first = 0; //first value to compare with.
for(int j = 0; j < Processes; j++){ // to make sure that it passes through all elements.
for(int i = 0; i < Processes; i++){ // pass each PID and Burst time to vector temp and temp1.
if(ProcessDetails[i].ArrivalTime == first){
temp.operator[](j) = ProcessDetails[i].PID;
temp1.operator[](j) = ProcessDetails[i].BurstTime;
}
}
first++;// increase first value to declare the round is finished and start a new one.
}
for(int i = 0; i < Processes; i++){ // pass the sorted vector values back to the arrays.
ProcessDetails[i].PID = temp.operator[](i);
ProcessDetails[i].BurstTime = temp1.operator[](i);
}
}
程序运行正常,直到达到这个功能,请帮助。
你的向量没有元素。
因此,使用矢量运算符[] 将失败。
使用 push_back、放置、调整大小或 some other function 向矢量添加元素。
您必须将矢量分配更改为
if(ProcessDetails[i].ArrivalTime == first){
temp.push_back(ProcessDetails[i].PID);
temp1.push_back(ProcessDetails[i].BurstTime);
}
如果向量用于访问不存在的元素,则向量 operator[]()
的行为未定义。
由于您使用了默认构造的向量,它们的大小为零 - 因此它们没有可访问的元素。
如果你使用.at()
成员函数,它会检查索引并在索引时抛出一个异常(类型std::out_of_range
,在标准头文件<stdexcept>
中声明)无效。您可以通过将代码包装在适当的 try
/catch
块中来确认这一点。
要消除该问题,您需要在使用 operator[]()
之前重新调整向量大小(例如,使用 push_back()
向其添加元素,使用 resize()
调整大小等)。并确保索引有效,因为 operator[]()
不会调整 std::vector
.
此外,temp[j]
等同于 temp.operator[](j)
。对于提供 operator[]()
函数的类型,编译器会将 temp[j]
等表达式转换为 temp.operator[](j)
.