0

我从以下代码中得到一个断言失败“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 循环

4

1 回答 1

2

您的循环执行iR = Ready.erase(iR);,然后仍然执行iR++。这是错误的,如果删除的元素是最后一个;你去一个结束。

常见的成语是:

for ( ; iR != end; ) { // <-- NO iR++
   if (condition_to_erase)
      iR = Ready.erase(iR);
   else
      iR++;
}
于 2013-10-23T09:25:52.217 回答