gpt4 book ai didi

c++ - 正在释放的指针未分配错误?

转载 作者:行者123 更新时间:2023-11-28 07:20:33 24 4
gpt4 key购买 nike

这个错误我看过很多帖子。但我没有动态保留内存或在析构函数中做任何事情:本程序为操作系统选择柱面的SSJF算法。

我有一个名为 IO 的简单类:

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;

然后我尝试做一些操作。我删除了一些不相关的部分。

    //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)
{

}

错误信息:

malloc:* 对象 0x10f68b3e0 错误:未分配正在释放的指针*在malloc_error_break中设置断点调试

任何人都可以帮助我并解释为什么我会收到此错误以及如何跳过它?

最佳答案

我能想到的重现此错误的最简单代码如下所示:

#include <list>

int main()
{
std::list<int> mylist;
mylist.pop_front();
}

我可以通过以下方式防止错误:

#include <list>

int main()
{
std::list<int> mylist;
if (!mylist.empty())
{
mylist.pop_front();
}
}

你正在打电话:

myList.pop_front();

...在一个 while 循环中,它又在一个 while 循环中,该循环还调用 myList.pop_front() .

我只能建议您调试您的代码以查看为mylist 调用了多少次pop_front()。我的钱在它上面,超过 mylist.size() 倍,hence my question in the comments (新重点):

How many items are in myList when the error is thrown?

也许最简单的解决方法是替换...

    //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())
{
}

...但是很难从您提供的代码片段中判断出来。

关于c++ - 正在释放的指针未分配错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19546982/

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