访问冲突读取位置0xCDCDCDCD

时间:2016-04-30 03:35:01

标签: c arrays linked-list

正如标题所述,我收到了错误

  

访问冲突读取位置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%d\n", "\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 个答案:

答案 0 :(得分:2)

  1. 通过malloc()分配的未初始化缓冲区中的值已分配给newNode->edge中的newNode->next = myGraph.edges[i];
  2. newNode通过currentmyGraph.edges[i] = newNode;设置为EdgeList current = myGraph.edges[i];
  3. 假设malloc()成功,current不是NULL,所以它正在进入循环。
  4. 1中分配的未初始化值已分配到current中的current = current->next;
  5. 使用通过malloc()分配的缓冲区中的值并在current != NULL处取消初始化来调用未定义的行为
  6. 要解决此错误,请以这种方式初始化myGraph.edges

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

    此外,将有害的强制转换移至int返回的指针的malloc()。明确地将返回值强制转换为指针也是not considered as good