gpt4 book ai didi

C:图中的邻接列表 - 段错误

转载 作者:行者123 更新时间:2023-11-30 15:48:15 25 4
gpt4 key购买 nike

注意:我不能使用“.”和图表上的“->”运算符。这就是我使用这些宏的原因。

对于以下图形声明(我无法更改 - 赋值,所以请不要问我任何相关问题),

#include <stdio.h>
#include <stdlib.h>
#define TAG(vp) ((vp)->tag)
#define LABEL(vp) ((vp)->label)
#define EDGE(vp) ((vp)->edge)

typedef struct vertex
{
char tag; /* can be used for any puproses; haven't used though */
char *label;
struct vertex *edge[1];
}
vertex, *vp;

我编写了以下结构和函数来为图创建邻接列表。

typedef struct adjList
{
vp node;
struct adjList *next;
}
adjList;


void createList (adjList *list, vp graph, int *place) /* place stores an index in an array of adjacency lists */
{
int i, temp = *place;
adjList *ptr;
if (graph)
{
list[temp].node = graph;
list[temp].next = NULL;
if (EDGE (graph))
{
ptr = list[temp].next;
for (i = 0; EDGE (graph)[i]; ++i)
{
ptr = malloc (sizeof (adjList));
ptr->node = EDGE (graph)[i];
ptr = ptr->next;
}
}
++(*place);
list = realloc (list, sizeof (adjList) * (*place + 1));
for (i = 0; EDGE (graph)[i]; ++i)
{
createList (list, EDGE (graph)[i], place);
}
}
}

使用这个 main 时,我遇到了段错误。

int main()
{
int i;
int *temp = malloc (sizeof (int));
adjList *list, *ptr;
vp test;
*temp = 0; /* temp is an index starting from 0 */
test = malloc (sizeof (*test) + 4 * sizeof (vp));
list = malloc (sizeof (adjList));
LABEL (test) = malloc (sizeof (char));
LABEL (test)[0] = 'a';
for (i = 0; i < 3; ++i)
{
EDGE (test)[i] = malloc (sizeof (vertex));
}
LABEL (EDGE (test)[0]) = malloc (sizeof (char));
LABEL (EDGE (test)[0])[0] = 'b';
LABEL (EDGE (test)[1]) = malloc (sizeof (char));
LABEL (EDGE (test)[1])[0] = 'c';
LABEL (EDGE (test)[2]) = malloc (sizeof (char));
LABEL (EDGE (test)[2])[0] = 'd';
EDGE (EDGE (test)[0])[0] = NULL;
EDGE (EDGE (test)[1])[0] = NULL;
EDGE (EDGE (test)[2])[0] = NULL;
EDGE (test)[3] = NULL;
printf ("%d\n", sizeof (EDGE (test)) / sizeof (vp));
createList (list, test, temp);
list = realloc (list, sizeof (adjList) * (*temp));
printf ("%c\n", LABEL (test)[0]);
printf ("%d\n", (test == list[0].node));
for (ptr = list; ptr; ptr = ptr->next)
{
printf ("%c ", LABEL (ptr->node)[0]);
}
printf ("\n");
return 0;
}

在调试它时,我发现创建邻接列表的函数甚至没有在“adjList”结构中存储指针。也许我没有正确分配内存。任何一点帮助将不胜感激。

提前致谢。

最佳答案

在 Mac OS X 上运行,我收到消息:

graph(70806) malloc: *** error for object 0x7fb211c03b20: pointer being realloc'd was not allocated

使用这个带注释的 createList() 函数版本:

static
void createList (adjList *list, vp graph, int *place) /* place stores an index in an array of adjacency lists */
{
int i, temp = *place;
adjList *ptr;
if (graph)
{
printf("-->> %s()\n", __func__);
list[temp].node = graph;
list[temp].next = NULL;
if (EDGE (graph))
{
ptr = list[temp].next;
for (i = 0; EDGE (graph)[i]; ++i)
{
ptr = malloc (sizeof (adjList));
ptr->node = EDGE (graph)[i];
ptr = ptr->next;
}
}
++(*place);
printf("About to realloc() in createList()\n");
list = realloc (list, sizeof (adjList) * (*place + 1));
printf("Back from realloc() in createList()\n");
for (i = 0; EDGE (graph)[i]; ++i)
{
printf("Recursing in createList\n");
createList (list, EDGE (graph)[i], place);
printf("Back from recursive createList\n");
}
printf("<<-- %s()\n", __func__);
}
}

运行的输出是:

1
-->> createList()
About to realloc() in createList()
Back from realloc() in createList()
Recursing in createList
-->> createList()
About to realloc() in createList()
Back from realloc() in createList()
<<-- createList()
Back from recursive createList
Recursing in createList
-->> createList()
About to realloc() in createList()
graph(70904) malloc: *** error for object 0x7fefca403b20: pointer being realloc'd was not allocated
*** set a breakpoint in malloc_error_break to debug

问题在于createList()重新分配传入的list,但无法告诉调用代码新列表的地址。您需要修改 createList() 以返回新列表,或者安排将 adjList **list 传递到函数中 - 无论哪种情况,都会进行相应的更改.

关于C:图中的邻接列表 - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16885861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com