这是如何导致分段错误(短代码)(C语言)

发布于 2025-01-13 17:55:34 字数 1525 浏览 3 评论 0原文

这导致了我的分段错误,每当我注释掉 breadthFirst =queue_enqueue(breadthFirst,n) 或 breadthFirst =queue_dequeue(breadthFirst) 时,我不再遇到 seg 错误,但它显然无法再解决我的任务。

我想除非你知道数据类型是如何实现的,否则可能很难帮助我,但我想我可以尝试一下,看看它是否是我明显遗漏的东西。

bool find_path(graph *g,node *src, node *dest) {

 
  
  queue *breadthFirst = queue_empty(NULL);

  breadthFirst = queue_enqueue(breadthFirst,src);

  while (!queue_is_empty(breadthFirst))` {
    void *v = queue_front(breadthFirst);

    g = graph_node_set_seen(g,v,1);

    dlist *neighbours = graph_neighbours(g,v);
    dlist_pos pos = dlist_first(neighbours);

    while (!(dlist_is_end(neighbours,pos))) {
        void *n = dlist_inspect(neighbours,pos);
        if (graph_node_is_seen(g,n)) {

        }
        else {
        breadthFirst = queue_enqueue(breadthFirst,n);
        }
        pos = dlist_next(neighbours,pos);
    }

   breadthFirst = queue_dequeue(breadthFirst);
  }
}




struct queue {
    list *elements;
};

queue *queue_empty(free_function free_func)
{
    // Allocate the queue head.
    queue *q=calloc(1, sizeof(*q));
    // Create an empty list.
    q->elements=list_empty(free_func);

    return q;
}

queue *queue_enqueue(queue *q, void *v)
{
    list_insert(q->elements, v, list_end(q->elements));
    return q;
}

queue *queue_dequeue(queue *q)
{
    list_remove(q->elements, list_first(q->elements));
    return q;
}

void *queue_front(const queue *q)
{
    return list_inspect(q->elements, list_first(q->elements));
}

根据请求我添加了队列实现

This is causing me segmentation fault, whenever i comment out breadthFirst = queue_enqueue(breadthFirst,n) or breadthFirst = queue_dequeue(breadthFirst) i no longer get seg fault but it obviously can't solve my task anymore.

I suppose it might be very hard to help me with this unless you know how the datatypes are implemented but i thought i might give it a shot and see if it is something obvious im missing.

bool find_path(graph *g,node *src, node *dest) {

 
  
  queue *breadthFirst = queue_empty(NULL);

  breadthFirst = queue_enqueue(breadthFirst,src);

  while (!queue_is_empty(breadthFirst))` {
    void *v = queue_front(breadthFirst);

    g = graph_node_set_seen(g,v,1);

    dlist *neighbours = graph_neighbours(g,v);
    dlist_pos pos = dlist_first(neighbours);

    while (!(dlist_is_end(neighbours,pos))) {
        void *n = dlist_inspect(neighbours,pos);
        if (graph_node_is_seen(g,n)) {

        }
        else {
        breadthFirst = queue_enqueue(breadthFirst,n);
        }
        pos = dlist_next(neighbours,pos);
    }

   breadthFirst = queue_dequeue(breadthFirst);
  }
}




struct queue {
    list *elements;
};

queue *queue_empty(free_function free_func)
{
    // Allocate the queue head.
    queue *q=calloc(1, sizeof(*q));
    // Create an empty list.
    q->elements=list_empty(free_func);

    return q;
}

queue *queue_enqueue(queue *q, void *v)
{
    list_insert(q->elements, v, list_end(q->elements));
    return q;
}

queue *queue_dequeue(queue *q)
{
    list_remove(q->elements, list_first(q->elements));
    return q;
}

void *queue_front(const queue *q)
{
    return list_inspect(q->elements, list_first(q->elements));
}

As per request i added the queue implementation

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

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

发布评论

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

评论(2

酒绊 2025-01-20 17:55:34

尝试修改您的 queue_empty 函数,如下所示:

queue *queue_empty(free_function free_func)
{
    // Allocate the queue head.
    queue *q=calloc(1, sizeof(queue));
    // Create an empty list.
    q->elements=list_empty(free_func);

    return q;
}

Try modifying your queue_empty function like this :

queue *queue_empty(free_function free_func)
{
    // Allocate the queue head.
    queue *q=calloc(1, sizeof(queue));
    // Create an empty list.
    q->elements=list_empty(free_func);

    return q;
}
枕花眠 2025-01-20 17:55:34

解决了!谢谢你的提示,我提供的信息不足以让任何人真正帮助我。我做了一些调试,发现我基本上误解了我的实现。

结构中的一个值

*n 是我发送的 *src

,是一个带有值和列表的节点,因此它在第一次迭代中工作正常,但随后我试图从 *n 中找到一个列表,它只是一个 void * value 并且不是结构或列表。然后我的程序尝试首先使用该列表并导致分段错误。

修复方法是使用 n 值来使用我拥有的函数,以找到与该值对应的结构。

Solved it! Thx for the tips tho, the information i presented was not enough for anyone to help me really. I did some debugging and found that i had misunderstood my implementations basically.

*n is a value in a struc

the *src i send in, is a node with a value and list

so it works fine on the first iteration, but then im trying to find a list from *n which is only just a void *value and is not a struc or a list. Then my program attempts to use first on whatever that list is and that caused segmentation fault.

The fix is to with the n value to use a function i have, to find the struct corresponding to that value.

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