-4

Okay so I have this issue:

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.

This occurs at this line of code using the debugger:

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

This is all the code for this specific part:

    /* 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;
    }  

This is my edge structure and my edges struct:

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;

So, I am trying to add an EDGE to an array of EDGES named 'edges'.

Please Help!

Borovez
  • 89
  • 1
  • 2
  • 13

3 Answers3

1

Problem is with your

edges.tail->next

You have to make sure if

edges.tail 

and

edges.tail->next

are not NULL before accessing it.

ravi
  • 10,994
  • 1
  • 18
  • 36
1

You need to test all variables and members for validity before you assign to them including tail and its members.

0xCDCDCD.... is a Microsoft runtime uninitialized memory marker. See here

Community
  • 1
  • 1
DNT
  • 2,356
  • 14
  • 16
  • Well if you read my code, I have something commented out... I have this edges.tail->next = new hull::EDGE; which also causes a violation error – Borovez Nov 09 '14 at 15:38
  • Does 'tail' have a correct value? You assign to edges.tail->next directy. – DNT Nov 09 '14 at 15:39
  • tail isnt supposed to have a value.... its an empty list... I want to add a value to it. – Borovez Nov 09 '14 at 15:41
  • Yes, but first tail needs to point to a struct line before you can access the 'line's next member. Did you check that 'tail' actually has been assigned to a 'line' ? – DNT Nov 09 '14 at 15:43
0

Ok I figured it out, you guys were on the right track for allocating memory... so ill give you a point for that.

I need to do this before I add to tail and tail->next:

    edges.tail = (hull::EDGE*) malloc(sizeof(hull::EDGE));
    edges.tail->next = (hull::EDGE*) malloc(sizeof(hull::EDGE));

Thanks.

Borovez
  • 89
  • 1
  • 2
  • 13