c-读取位置0xCDCDCDCD的访问冲突



正如标题所述,我得到了一个错误

读取位置0xCDCDCDCD时发生访问冲突。

现在我正在处理一系列链表,我认为问题在于添加到链表中。我通常对此很满意,但我觉得我在内存分配方面做得不对。

以下是我的结构:

图表:

typedef struct graph
{
    int V;
    int *state;
    EdgeList *edges;
} Graph;

边缘:

typedef struct edge
{
    int toVertex;
    int weight;
} Edge;

EdgeList:

typedef struct edgeNode
{
    Edge edge;
    struct edgeNode *next;
} *EdgeList;

以下是运行这一切的主要功能:

main()
{
    Graph myGraph;
    scanf("%d", &(myGraph.V));
    myGraph.state = (int)malloc(myGraph.V*sizeof(int));
    myGraph.edges = (EdgeList*)malloc(myGraph.V*sizeof(EdgeList));
    int *inDegrees;
    inDegrees = (int)malloc(sizeof(int)*myGraph.V);
    /*  Sets all array values to 0  */
    for (int counter = 0; counter < myGraph.V; counter++)
    {
        inDegrees[counter] = 0;
    }

    for (int i = 0; i < myGraph.V; i++)
    {
        int number_of_edges;
        int input = 0;  /*For that little experimental bit*/
        scanf("%d", &(myGraph.state[i]));
        scanf("%d", &number_of_edges);
        if (number_of_edges > 0)
        {
            for (int j = 0; j < number_of_edges; j++)
            {
                Edge newEdge;
                scanf("%d,%d", &(newEdge.toVertex), &(newEdge.weight));
                inDegrees[newEdge.toVertex]++;
                printf("%s%dn", "nOoh, new input for ", newEdge.toVertex);
                /*insert at front*/
                EdgeList newNode = (EdgeList)malloc(sizeof (struct edgeNode));
                newNode->edge = newEdge;
                newNode->next = myGraph.edges[i];
                myGraph.edges[i] = newNode;

                /* Bit to calculate state.*/
                EdgeList current = myGraph.edges[i];
                while (current != NULL)
                {
                    if (current->edge.toVertex == i)
                    {
                        input += (current->edge.weight)*(myGraph.state[i]);
                    }
                    current = current->next;
                }
            }
            if (input > 0)
            {
                myGraph.state[i] = 1;
            }
            else
            {
                myGraph.state[i] = 0;
            }
        }
    }
    //print
    for (int k = 0; k < myGraph.V; k++)
    {
        printf("n%s%d%s", "In degrees for ", k, ": ");
        printf("%d", inDegrees[k]);
    }
}

特别是,错误发生在遍历链表的过程中。它在上面的代码中,但我将在这里强调它:

EdgeList current = myGraph.edges[i];
while (current != NULL)
{
    if (current->edge.toVertex == i)
    {
        input += (current->edge.weight)*(myGraph.state[i]);
    }
    current = current->next;
}

如果有人能帮忙,我将不胜感激,因为我实在太困了。

  1. 通过malloc()分配的未初始化缓冲区中的值被分配给newNode->next = myGraph.edges[i];中的newNode->edge
  2. 经由myGraph.edges[i] = newNode;EdgeList current = myGraph.edges[i];newNode设置为current
  3. 假设malloc()成功,那么current在这里不是NULL,所以它正在进入循环
  4. 1中assigned的未初始化值被分配给current = current->next;中的current
  5. 未定义的行为通过使用通过malloc()分配且在current != NULL未初始化的缓冲区中的值来调用

要修复此错误,请在例如中初始化myGraph.edges

myGraph.edges = (EdgeList*)malloc(myGraph.V*sizeof(EdgeList));
for (int i = 0; i < myGraph.V; i++)
{
    myGraph.edges[i] = NULL;
}

此外,请删除从malloc()返回的指针对int的有害强制转换。将返回值显式转换为指针也不太好。

最新更新