简体   繁体   English

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

[英]Getting a strange seg fault from imported queue.front()

So I am currently working on a graph abstract data type for my CS163 class. 因此,我目前正在为CS163类研究图形抽象数据类型。 Everything else for this program works awesome BUT the depth-first traversal of the graph. 该程序的其他所有功能都非常出色,但是图形的深度优先遍历。 When testing, I add all the vertices I want, then connect them as I need to. 测试时,我添加了所有想要的顶点,然后根据需要连接它们。 I check and everything is indeed linked as it should be so we are good to go. 我检查了一下,所有的事情确实都应该链接在一起,所以我们一切顺利。 I jump into this function below, given the name of the friend(vertex) to start from. 我从下面跳入此函数,给定了要从其开始的friend(vertex)的名称。 The line I commented "*current_buddy = myQueue.front();" 我评论的行“ * current_buddy = myQueue.front();” is the line I found in GDB to be the offending piece of code that causes a seg fault. 我在GDB中发现的那行是引起段错误的有问题的代码。 Also using GDB, I was able to successfully print the proper vertex using "p myQueue.front()" 同样使用GDB,我能够使用“ p myQueue.front()”成功打印正确的顶点

So perhaps a sample test to give a bit more context. 因此,也许可以通过样本测试给出更多背景信息。 say I have the points a,b,c,d, and e. 说我有a,b,c,d和e点。 a is connected to c,d, and e. a连接到c,d和e。 b is connected to c. b连接到c。 I want to start at vertex "a" for this test. 我想从该测试的顶点“ a”开始。 Passing in a to the function, it finds the proper index with the find_location function which simply returns said index integer. 传递给函数,它使用find_location函数查找正确的索引,该函数仅返回所述索引整数。 It will then push that vertex onto the queue and mark that vertex as visited so that upon traversal, I don't return to that point to push it onto the queue again. 然后它将将该顶点推入队列并将该顶点标记为已访问,以便遍历时,我不会返回该点再次将其推入队列。 I create a node "current" that is attached to the first node in the edge list for the "a" vertex. 我创建一个“当前”节点,该节点附加到“ a”顶点的边缘列表中的第一个节点上。 When it goes into the first loop, I use the previously created vertex "current_buddy" to point to the first object in the queue, which is the vertex "a." 当它进入第一个循环时,我使用先前创建的顶点“ current_buddy”来指向队列中的第一个对象,即顶点“ a”。 This is the point that the program hits a seg fault and I cannot for the life of me figure out what is going on with that line of code that would cause this. 这就是程序遇到段错误的原因,我无法终生弄清楚导致该错误的那一行代码是怎么回事。

Both "vertex" and "node" are structs I have created and the queue I am using is from the standard library #include queue more on the queue bit here if needed Any and all information would be greatly appreciated! “ vertex”和“ node”都是我创建的结构,我正在使用的队列来自标准库#include队列,如果需要的话,请在这里的更多队列位上。任何和所有信息将不胜感激! Obviously since this is a school assignment, I don't expect anyone to just give me the answer but I am lost at the moment. 显然,由于这是学校的任务,所以我不希望有人给我答案,但是我现在迷路了。

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;
}

我不知道这是否会按您期望的那样工作,但是一个快速的解决方法可能是使用操作数的地址并实际分配指针而不是取消对它的引用:

current_buddy = &myQueue.front();

(EDIT: For the first part of this question, the solution/answer provided by Joachim may perform better. I just like avoid pointer when possible.) (编辑:对于该问题的第一部分,Joachim提供的解决方案/答案可能会表现更好。我喜欢尽可能避免使用指针。)

You could try: 您可以尝试:

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;

BTW - I think you need to check for null here: 顺便说一句-我认为您需要在此处检查null:

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

So that you don't get another crash 这样您就不会再崩溃

This part 这部分

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

            current = current->next;
        }

    }

looks like an endless loop in case current->connected_buddy->visited is true. current->connected_buddy->visited为true的情况下current->connected_buddy->visited看起来像是一个无限循环。

Maybe you want 也许你想要

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

        }

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

It's segfault because you are attempting to deference a NULL pointer. 这是段错误,因为您尝试引用NULL指针。

vertex * current_buddy = NULL;

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

You should change it to 您应该将其更改为

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

while(myQueue.empty() == false)
{
    vertex *current_buddy = &myQueue.front();

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

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