从导入的queue.front()中获取奇怪的seg错误

时间:2015-12-04 07:20:26

标签: c++ algorithm segmentation-fault breadth-first-search

所以我目前正在研究CS163类的图形抽象数据类型。此程序的其他所有工作都很棒但是图表的深度优先遍历。测试时,我添加了我想要的所有顶点,然后根据需要连接它们。我检查,确实一切都是如此,因此我们很乐意去。给定从头开始的朋友(顶点)的名称,我跳到下面的这个函数。我评论过的行" * current_buddy = myQueue.front();"我在GDB中找到的行是导致seg错误的有问题的代码段。同样使用GDB,我能够使用" p myQueue.front()"

成功打印正确的顶点。

所以也许是一个样本测试来提供更多的上下文。说我有点a,b,c,d和e。 a连接到c,d和e。 b连接到c。我想从顶点开始" a"对于这个测试。传递给函数,它使用find_location函数找到正确的索引,该函数只返回所述索引整数。然后它会将该顶点推入队列并将该顶点标记为已访问,以便在遍历时,我不会返回到该点再次将其推入队列。我创建了一个节点" current"附加到边缘列表中第一个节点的" a"顶点。当它进入第一个循环时,我使用先前创建的顶点" current_buddy"指向队列中的第一个对象,即顶点" a。"这是程序遇到一个seg错误的一点,我不能在我的生活中找出导致这种情况发生的代码行。

两者"顶点"和"节点"是我创建的结构,我使用的队列来自标准库#include queue more on the queue bit here if needed任何和所有信息都将非常感谢!显然,由于这是一项学校作业,我不希望任何人给我答案,但我现在迷失了。

bool graph::breadth_first(char * start)
{
    if(adjacency_list[0].buddy_name == NULL)
        return false;

    int location = find_location(start);
    queue<vertex> myQueue;

    myQueue.push(adjacency_list[location]);
    adjacency_list[location].visited = true;

    node * current = adjacency_list[location].head;
    vertex * current_buddy = NULL;

    while(myQueue.empty() == false)
    {
        *current_buddy = myQueue.front();//THIS LINE SEG FAULTS
        cout << "THIS IS A FRIEND IN THE BREADTH-FIRST TRAVERSAL" << current_buddy->buddy_name << endl;
        current = current_buddy->head;
        myQueue.pop();
        while(current != NULL)
        {
            if(current->connected_buddy->visited == false)
            {
                current_buddy = current->connected_buddy;
                location = find_location(current_buddy->buddy_name);
                myQueue.push(adjacency_list[location]);
                adjacency_list[location].visited = true;
                current = current->next;
            }

        }
    }

    for(int i = 0; adjacency_list[i].buddy_name != NULL; ++i)
    {
        adjacency_list[i].visited = false;
    }
    return true;
}

3 个答案:

答案 0 :(得分:2)

我不知道这是否会按预期工作,但快速修复可能是使用操作数地址并实际分配指针而不是取消引用它:

current_buddy = &myQueue.front();

答案 1 :(得分:1)

(编辑:对于这个问题的第一部分,Joachim提供的解决方案/答案可能表现得更好。我只是想尽可能避免指针。)

你可以尝试:

vertex * current_buddy = NULL;

while(myQueue.empty() == false)
{
    vertex front_buddy = myQueue.front();
    cout << "THIS IS A FRIEND IN THE BREADTH-FIRST TRAVERSAL" << front_buddy.buddy_name << endl;
    current = front_buddy.head;

顺便说一句 - 我想你需要在这里检查一下:

    while(current != NULL)
    {
        if (current->connected_buddy == NULL)
        {
            // add error handling
        }
        else
        {
            // normal code
        }
     }

这样你就不会再次崩溃

这部分

   while(current != NULL)
    {
        if(current->connected_buddy->visited == false)
        {
            // your code

            current = current->next;
        }

    }

current->connected_buddy->visited为真的情况下看起来像无限循环。

也许你想要

   while(current != NULL)
    {
        if(current->connected_buddy->visited == false)
        {
            // your code

        }

        // Moved out of the if-statement
        current = current->next;
    }

答案 2 :(得分:0)

它是段错误,因为您正在尝试使用NULL指针。

vertex * current_buddy = NULL;

while(myQueue.empty() == false)
{
    // V de-referencing a NULL pointer
    *current_buddy = myQueue.front();//THIS LINE SEG FAULTS

您应该将其更改为

// vertex * current_buddy = NULL; // < Remove this line

while(myQueue.empty() == false)
{
    vertex *current_buddy = &myQueue.front();
相关问题