c++ - 指向结构体访问冲突写入位置的指针

标签 c++ c pointers struct access-violation

好的,我有这个问题:

Unhandled exception at 0x00261A46 in CompGeometry.exe: 0xC0000005: Access violation writing location 0xCDCDCDED.
First-chance exception at 0x00261A46 in CompGeometry.exe: 0xC0000005: Access violation writing location 0xCDCDCDED.

这发生在使用调试器的这行代码中:

edges.tail->next->a = newEdge->a;

这是该特定部分的所有代码:

    /* non-empty list -- add it */
    if (edges.head != NULL) 
    {   
       printf("1 %d, %d\n", newEdge->a.x, newEdge->a.y);
       printf("2 %d, %d\n", newEdge->b.x, newEdge->b.y);

       //edges.tail->next = new hull::EDGE;

       edges.tail->next->a = newEdge->a;
       edges.tail->next->b = newEdge->b;
       newEdge->next = NULL;
       edges.tail->a.x = newEdge->a.x;
       edges.tail->a.y = newEdge->a.y;
       edges.tail->b.x = newEdge->b.x;
       edges.tail->b.y = newEdge->b.y;
    }
    /* empty list -- 1st item */
    else
    { 
       edges.head = newEdge;
       edges.tail = newEdge;
       newEdge->next = NULL;
    }  

这是我的边缘结构和我的边缘结构:

typedef struct line {
    VERTEX a;
    VERTEX b;
    struct line *next;
} EDGE;

struct edges {
    int size;
    EDGE* head;
    EDGE* tail;
};

//all edges of the hull
struct edges edges;

因此,我尝试将一条 EDGE 添加到名为“edges”的 EDGES 数组中。

请帮忙!

最佳答案

问题出在你的

edges.tail->next

你必须确定是否

edges.tail 

edges.tail->next

在访问它之前不为 NULL。

关于c++ - 指向结构体访问冲突写入位置的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26829824/

相关文章:

c++ - 条件 C/C++ 结构定义

c++ - 为什么我应该使用指针而不是对象本身?

c - 用随机数填充数组

c++17倍表达式语法

c++ - unistd.h read() 正在读取更多数据然后被写入

c++ - 带有异步 C++ 驱动程序的 SQL/NoSQL 数据库管理系统

c - 了解 C 异常程序

c - 在c中反转链表时出现段错误问题

c++ - 这个指针和所有参数在调用后都是空的(但之前没问题)

C指针和内存泄漏