简体   繁体   English

push_back上的STL列表分段错误

[英]STL list segmentation fault on push_back

I'm having a problem when using STL's list. 使用STL的列表时出现问题。 I'm loading a digraph in format 我正在以格式加载有向图

#of_tests
#of_vertexes #of_edges
#vertex1 > #vertex2
#vertex3 > #vertex4
...

But I get a SIGSEGV on first call of push_back on my adjacency lists, and I'm really baffled - the array of lists is initialized, so I don't call it on null. 但是我在邻接表上第一次调用push_back时得到了SIGSEGV,我真的很困惑-列表数组已初始化,因此我不将其称为null。

I've checked the tests, and I'm within bounds (I never call a method beyond allocated array). 我已经检查了测试,并且处于界限之内(我从不调用超出分配数组的方法)。

Here's the code 这是代码

#include <iostream>
#include <list>

using namespace std;

int * deg_in;
list<int> * edge;
int n;

int main()
{
    int z;
    cin >> z;

    deg_in = new int[n]();
    edge = new list<int>[n]();

    while(z--)
    {
        int m;
        cin >> n >> m;

        while(m--)
        {
            int a, b;
            char trash;
            cin >> a >> trash >> b;

            /// Vertexes are given 1 .. n, but stored 0 .. n - 1
            a--;
            b--;

            edge[a].push_back(b);   /// code fails here
            deg_in[b]++;
        }
/// do somethig with graph


    delete [] deg_in;
    delete [] edge;
}


    return 0;
}

Any help is appreciated. 任何帮助表示赞赏。

You're deleting your data within the loop, so you'll crash on the second iteration. 您正在循环中删除数据,因此在第二次迭代时会崩溃。 It's not obvious because the code is badly formatted. 这不是很明显,因为代码格式错误。 Change: 更改:

/// do somethig with graph


    delete [] deg_in;
    delete [] edge;
}

to: 至:

/// do somethig with graph

    }
    delete [] deg_in;
    delete [] edge;

Alternatively you can allocate and de-allocate within the loop instead. 或者,您可以在循环内分配和取消分配。

Take-home message: always take care to format your code properly, otherwise you're more likely to make simple hard-to-spot mistakes like this. 提示:请务必谨慎设置代码格式,否则您将更容易犯此类难以发现的简单错误。

Your code allocates the deg_in and edge arrays before inputting n . 您的代码在输入n之前分配deg_inedge数组。 Since n is declared in global scope, it is initialized to zero and thus the arrays are of length 0. The SIGSEGV thus arises because the program tries to access an unallocated part of memory. 由于n是在全局范围内声明的,因此它将初始化为零,因此数组的长度为0。之所以出现SIGSEGV,是因为程序尝试访问内存的未分配部分。

In addition, you delete the arrays immediately after trying to process the first test case and the arrays are not re-allocated for every test case. 此外,在尝试处理第一个测试用例后,您将立即删除阵列,并且不会为每个测试用例重新分配阵列。

From the context, it seems likely that the deg_in and edge arrays are meant to be per-testcase. 从上下文来看, deg_inedge数组似乎是针对每个测试用例的。 In that case, the code should be: 在这种情况下,代码应为:

while (z--)
{
    int m;
    cin >> n >> m;

    deg_in = new int[n]();
    edge = new list<int>[n]();

    // input graph

    delete [] deg_in;
    delete [] edge;
}

Like Paul R said, formatting your code consistently help reduce the chance of simple mistakes. 就像Paul R所说的那样,始终如一地格式化代码有助于减少简单错误的机会。 Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM