-2

对于以下图形声明(我无法更改 - 赋值;还有宏,因为我不允许对图形使用 '.' 和 '->' 运算符)

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

typedef struct vertex 
{
    char tag;
    char *label;
    struct vertex *edge[1];
}
vertex, *vp;

和这个邻接列表声明(对于这个和所有下一个代码,我可以做任何我想做的事情)

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

我写了一个函数的两个版本,它必须从图中创建邻接表表示。他们都使用此功能创建一个列表。

adjList *createList (vp graph)
{
    int i;
    adjList *result, *ptr, *ptr2;
    if (graph)
    {
        result = malloc (sizeof (adjList));
        result->node = graph;
        ptr2 = result;
        for (i = 0; EDGE (graph)[i]; ++i)
        {
            ptr = malloc (sizeof (adjList));
            ptr->node = EDGE (graph)[i];
            ptr2->next = ptr;
            ptr2 = ptr;
        }
        ptr2->next = NULL;
    }
    return result;
}

第一个返回一个邻接列表数组(作为双指针),但仅对一个节点及其子节点执行此操作(我还没有弄清楚如何使用此工作函数返回完整表示)

adjList **createLists (vp graph)
{
    int i;
    adjList **result;
    result = malloc (sizeof (adjList *));
    result[0] = createList (graph);
    for (i = 0; EDGE (graph)[i]; ++i)
    {
        result = realloc (result, sizeof (adjList *) * (i + 2));
        result[i + 1] = createList (EDGE (graph)[i]);
    }
    result = realloc (result, sizeof (adjList *) * (i + 2));
    result[i + 1] = NULL;
    return result;
}

第二个产生分段错误。基本上,它创建了一个空列表数组,但使用这个逻辑我会创建完整的表示。

void createListsHelper (adjList **result, vp graph, int *index) /* index stores an index in an array to create there next list */
{
    int i;
    if (graph)
    {
        result = realloc (result, sizeof (adjList *) * (*index + 2));
        result[*index] = createList (graph);
        for (i = 0; EDGE (graph)[i]; ++i)
        {
            ++(*index);
            createListsHelper (result, EDGE (graph)[i], index);
        }
    }
}

adjList **createLists (vp graph)
{
    adjList **result = malloc (sizeof (adjList *));
    int *index = malloc (sizeof (int));
    *index = 0;
    createListsHelper (result, graph, index);
    result[*index + 1] = NULL;
    return result;
}

我怎样才能改变其中之一,使它们能够正常工作。

提前致谢。

注意:我使用以下“主要”来测试它们。

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

1 回答 1

2
void createListsHelper (adjList **result, vp graph, int *index) /* index stores an index in an array to create there next list */
{
    int i;
    if (graph)
    {
        result = realloc (result, sizeof (adjList *) * (*index + 2));

由于result是传值( C语言中每个result参数都是传值),所以这个修改在调用函数中不会有影响。这条线

createListsHelper (result, graph, index);
result[*index + 1] = NULL; /* <<< */

调用 UB (因为result仍然指向 1 的数组int,并且您正在访问第二个元素)。

于 2013-06-04T14:08:10.107 回答