我从下面的代码中得到一个断言失败“list iterator not dereferencable”:
它说它来自最后一行。
void cpuschedule::Sjf()
{
int CT = 0;
bool IO = false;
Data temp;
for(it = lPro.begin(); it != lPro.end(); ++it)
{
if(it->RQ)
{
temp.AT = it->AT;
temp.BUT = it->BUT;
temp.BUP = it->BUP;
temp.IOP = it->IOP;
temp.IOT = it->IOT;
temp.ProNum = it->ProNum;
temp.RQ = it->RQ;
temp.vData = it->vData;
Ready.push_back(temp);
}
}
it2 = Ready.begin();
while(!(Ready.empty()))
{
if(IO)
{
for(iR = Ready.begin(); iR != Ready.end(); ++iR)
{
if(!(iR->RQ))
{
(iR->IOT)--;
if(iR->IOT == 0)
{
iR->BUP += 2;
iR->IOP += 2;
iR->BUT = iR->vData[(iR->BUP)];
iR->IOT = iR->vData[(iR->IOP)];
iR->RQ = true;
iR->AT = CT;
}
if(iR->IOT == -121)//Check for the end of the Vector
{
temp.AT = iR->AT;
temp.BUT = iR->BUT;
temp.BUP = iR->BUP;
temp.IOP = iR->IOP;
temp.IOT = iR->IOT;
temp.ProNum = iR->ProNum;
temp.RQ = iR->RQ;
temp.vData = iR->vData;
Out.push_back(temp);//Instert to the List of Done
iR = Ready.erase(iR);
}
}
}
}
Ready.sort(comp);
if((it2->BUT != 0) && (it2->RQ))
{
it2->BUT--;
if(it2->BUT == 0)
{
printState(CT);
it2->RQ = false;
IO = true;
if(next(it2) == Ready.end())
{
it2 = Ready.begin();
}
else
++it2;
}
}
CT++;
}
}
这是CPU调度SJF的while循环。 它上升到691,它应该做901循环
答案 0 :(得分:2)
您的循环执行iR = Ready.erase(iR);
,然后执行iR++
。如果删除的元素是最后一个,那就错了;你一个接一个地去。
常见的习语是:
for ( ; iR != end; ) { // <-- NO iR++
if (condition_to_erase)
iR = Ready.erase(iR);
else
iR++;
}