C++ 中 pop_front() 的分段错误

发布于 2024-10-24 20:04:55 字数 800 浏览 1 评论 0原文

我的问题是,在调用“processList.pop_front()”后出现分段错误。如果我注释掉“processList.pop_front()”,则在 tt.pop_front() 调用之后、最外层 for 循环的第二次迭代期间会发生分段内部 for 循环已经进行了近 5000 次迭代。我看不出问题出在哪里。有什么想法吗?

loopLimit = processList.size();

for(int i = 0; i < loopLimit; i++)
{
    tempProcess = processList.front();
    tt = tempProcess.memAccesses;
    cout << "process number " << i << "\n";

    while(!tt.empty())
    {
        t = tt.front();
        tt.pop_front();
        cout << "from processlist: " << t.instrType << "  " << t.instrAddr << "\n";
    }

    if(!processList.empty())
    {
        cout << "size is now: " << processList.size() << "\n";
        processList.pop_front();
    }

}

My problem is that I am getting a segmentation fault that occurs after I call "processList.pop_front()". If I comment out "processList.pop_front()", a segmentation then occurs during the second iteration of the outermost for loop, after a tt.pop_front() call, after that inner for loop has gone through almost 5000 iterations. I can't see what the problem is. Any thoughts?

loopLimit = processList.size();

for(int i = 0; i < loopLimit; i++)
{
    tempProcess = processList.front();
    tt = tempProcess.memAccesses;
    cout << "process number " << i << "\n";

    while(!tt.empty())
    {
        t = tt.front();
        tt.pop_front();
        cout << "from processlist: " << t.instrType << "  " << t.instrAddr << "\n";
    }

    if(!processList.empty())
    {
        cout << "size is now: " << processList.size() << "\n";
        processList.pop_front();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脸赞 2024-10-31 20:04:55

我有一个类似的问题,我通过向双端队列添加一个无效值来解决它:

loopLimit = processList.size();

for(int i = 0; i < loopLimit; i++)
{
    tempProcess = processList.front();
    tt = tempProcess.memAccesses;
    cout << "process number " << i << "\n";

    tt.push_back(-1);  //I guess you don't have a -1 type, so you wont have colisions
    while( tt.front()!=-1)
    {
        t = tt.front();
        tt.pop_front();
        cout << "from processlist: " << t.instrType << "  " << t.instrAddr << "\n";
    }

    if(!processList.empty())


    {
        cout << "size is now: " << processList.size() << "\n";
        processList.pop_front();
    }

}

可能你需要稍微调整一下。
这不是一个很好的解决方案,但它确实有效。

I have a similar problem and I solved it by adding an invalid value to the deque:

loopLimit = processList.size();

for(int i = 0; i < loopLimit; i++)
{
    tempProcess = processList.front();
    tt = tempProcess.memAccesses;
    cout << "process number " << i << "\n";

    tt.push_back(-1);  //I guess you don't have a -1 type, so you wont have colisions
    while( tt.front()!=-1)
    {
        t = tt.front();
        tt.pop_front();
        cout << "from processlist: " << t.instrType << "  " << t.instrAddr << "\n";
    }

    if(!processList.empty())


    {
        cout << "size is now: " << processList.size() << "\n";
        processList.pop_front();
    }

}

Probably you will have to adapt it a bit.
Is not a very nice solution, but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文