I'm trying to learn linked lists, and I've ran into a weird problem.
My code gets accepted without errors and Game Editor can run it without problems,
but when I return back from Game Mode it takes a few seconds and then GE freezes and crashes.
Here's my code:
In Global Code:
- Code: Select all
int polygon[5][2] =
{
{300, 250},
{350, 230},
{375, 255},
{320, 300},
{275, 275}
};
typedef struct nodeStruct
{
int x;
int y;
struct nodeStruct * next;
}node;
node * buildPolygon(int cornerNumber, int pPointer[][2])
{
int i;
node * nc;
nc = malloc(cornerNumber * sizeof(struct nodeStruct));
for (i = 0; i < cornerNumber; i ++)
{
nc[i].x = pPointer[i][0];
nc[i].y = pPointer[i][1];
if (i < cornerNumber - 1)nc[i].next = &nc[i + 1];
else nc[i].next = NULL;
}
return &nc[0];
}
void drawPolygon(node * pointer)
{
node * first;
setpen(255, 255, 255, 0, 1);
first = pointer;
moveto(pointer->x, pointer->y);
while (pointer->next != NULL)
{
lineto(pointer->next->x, pointer->next->y);
pointer = pointer->next;
}
lineto(first->x, first->y);
free(pointer);
}
And in a canvas actor's create actor event:
- Code: Select all
drawPolygon(buildPolygon(5, polygon));
The problem is related to the function buildPolygon and to the dynamically allocated array of structs called node * nc.
I managed to get it working by using an array of pointers to structs, but since I don't actually have need for that I'd prefer using just an array of structs.
Is there something wrong with my code or is it a compiler bug?