访问冲突读取位置0xCDCDCDCD

用户名

如标题所述,我遇到了错误

访问冲突读取位置0xCDCDCDCD。

现在,我正在处理一系列链接列表,并且我认为麻烦在于围绕添加到链接列表中。我通常对此表示满意,但我觉得我在内存分配上做错了什么。

这是我的结构:

图形:

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

边缘:

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

边列表:

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. 通过malloc()分配的未初始化缓冲区中的值分配给newNode->edgein newNode->next = myGraph.edges[i];
  2. newNode设置为currentviamyGraph.edges[i] = newNode;EdgeList current = myGraph.edges[i];
  3. 假设malloc()成功current不在NULL这里,那么它就进入了循环。
  4. 分配给1的未初始化值分配给currentin current = current->next;
  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;
}

此外,请删除int从返回的指针的有害强制类型转换malloc()显式地将返回值强制转换为指针也被认为是不好的

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

链表:抛出异常:读取访问冲突。B 是 0xCDCDCDCD

在 file.exe 中的 0x7A2A93B6 (vcruntime140d.dll) 处引发异常:0xC0000005:访问冲突读取位置 0xCDCDCDCD

将值推送到队列前端(链表)时的写访问冲突(@ 0xCDCDCDCD)

在 filePath.exe 中的 0x793F3729 (vcruntime140d.dll) 处抛出异常:0xC0000005:访问冲突写入位置 0xCDCDCDCD

未处理的异常访问冲突写入位置0xCDCDCDCD-在Structure C ++中

异常错误:访问冲突读取位置0xDDDDDDDD

访问冲突读取位置0x00000014。使用流

访问冲突读取位置0xCDCDCDD1

访问冲突读取位置0x00000000 cstrings

访问冲突读取位置0x00000003

访问冲突读取位置0x012DEFFC。C ++

访问冲突读取位置fprintf

例外:“访问冲突读取位置”

CreateBuffer抛出“访问冲突读取位置”

使用Botan访问冲突读取位置

C ++ CImg访问冲突读取位置

vc++访问冲突读取位置

使用Cudd的访问冲突读取位置

#define导致“访问冲突读取位置”

未处理的异常:访问冲突读取位置

C++访问冲突读取位置错误

使类可迭代:访问冲突读取位置

realloc 抛出“访问冲突读取位置”

ifstream 读取期间访问冲突写入位置

多线程访问冲突读取位置C ++

访问冲突读取位置(原始指针变量)

链接列表中的访问冲突读取位置0xCCCCCCCC错误

0xC0000005:析构函数中的访问冲突读取位置错误

使用 __func__ 标识符时 C++ 访问冲突读取位置 0xFFFFFFFFFFFFFFFF