C++ 正在释放的指针未分配错误?
我已经看到很多关于这个错误的帖子。但我不会动态保留内存或在析构函数中执行任何操作: 本程序是SSJF算法,用于在操作系统中选择气缸 我有一个叫做IO的简单类:C++ 正在释放的指针未分配错误?,c++,list,C++,List,我已经看到很多关于这个错误的帖子。但我不会动态保留内存或在析构函数中执行任何操作: 本程序是SSJF算法,用于在操作系统中选择气缸 我有一个叫做IO的简单类: class IO { public: IO(); IO(int,int); void setIO(int,int); ~IO(); int trackNo; int arrival;
class IO
{
public:
IO();
IO(int,int);
void setIO(int,int);
~IO();
int trackNo;
int arrival;
int start;
int end;
bool finished;
};
下面是类的实现::
IO::IO(int arr, int tNum)
{
this->arrival = arr;
this->trackNo = tNum;
this->start = 0;
this->end = 0;
}
IO::IO()
{
}
IO::~IO()
{
}
void IO::setIO(int t1, int t2)
{
this->trackNo = t1;
this->arrival = t2;
}
最后是主程序的一部分:
list<IO> myList;
....
myList.push_back(tmpIO); //Add to the list
...
list<IO> wt_list;
列出myList;
....
myList.push_back(tmpIO)//添加到列表中
...
列表WTU列表;
然后我试着做一些手术。我删除了一些不相关的部分
//list<IO>::iterator itMin;
while(myList.size()>0)
{
//If it is the first input just get it
if(f)
{
IO selected = myList.front();
curr_time += selected.arrival + selected.trackNo;
f=false;
cout << selected.arrival<<endl;
lastPos = selected.trackNo;
myList.pop_front();
}
//Check if there is any item to add to queue
while(myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front(); //Error is coming from this line
}
while(wt_list.size()>0)
{
}
//列表::迭代器itMin;
而(myList.size()>0)
{
//如果它是第一个输入,就得到它
如果(f)
{
IO selected=myList.front();
当前时间+=selected.arrival+selected.trackNo;
f=假;
cout我能想出的重现此错误的最简单代码如下所示:
#include <list>
int main()
{
std::list<int> mylist;
mylist.pop_front();
}
…在while
-循环中,该循环又在while
-循环中,该循环也调用myList.pop_front()
我只能建议你调试你的代码,看看有多少次pop_front()
被mylist
调用。我的钱花在它上面的次数超过了mylist.size()
次,(新的重点是):
抛出错误时,myList中有多少项
也许最简单的修复方法是替换
//Check if there is any item to add to queue
while(myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front(); //Error is coming from this line
}
while(wt_list.size()>0)
{
}
//检查是否有任何项目要添加到队列中
while(myList.front().arrival0)
{
}
…与
while (!mylist.empty() && myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front();
}
while (!wt_list.empty())
{
}
while(!mylist.empty()&&mylist.front().arrival
…但是很难从您提供的代码片段中分辨出来。那么,您从哪里得到错误?您是否尝试过在调试器中运行您的程序?请发布准确的错误消息,并(如果可能)在代码示例中添加注释以显示哪一行错误是指。我添加了更多信息。错误来自我注释的第三部分。抛出错误时,myList
中有多少项?换句话说,就在抛出错误之前,调用myList.pop_front()
:什么是myList.size()
在那一点上?我真的很感谢你的回答。你是对的。经过几次迭代后,列表是空的……我想可能是这样。祝你好运!
//Check if there is any item to add to queue
while(myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front(); //Error is coming from this line
}
while(wt_list.size()>0)
{
}
while (!mylist.empty() && myList.front().arrival < curr_time)
{
wt_list.push_back(myList.front());
myList.pop_front();
}
while (!wt_list.empty())
{
}