Dynamically allocated array of structs

You must understand the Game Editor concepts, before post here.

Dynamically allocated array of structs

Postby lcl » Sun Mar 09, 2014 6:24 pm

Hi.

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?
Last edited by lcl on Tue Mar 11, 2014 12:13 am, edited 1 time in total.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Dynamically allocated array of structs / compiler bug in

Postby skydereign » Mon Mar 10, 2014 5:12 am

lcl wrote:but when I return back from Game Mode it takes a few seconds and then GE freezes and crashes.

It doesn't happen for me. The code runs fine, and from a cursory glance at it, it doesn't look like it should cause any problems. One thing though is you should free the memory when you are done with it (so when you exit the game), which might be causing some problems.

lcl wrote: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.

To clarify you are using a pointer to an array of structs, not an array of pointers to structs. An array of pointers to structs would be node* []. So technically what you have is an array of structs. Do you mean you don't want the pointer at all? If so then you can do that, it just won't be dynamic in size and you have to create it in advance.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Dynamically allocated array of structs / compiler bug in

Postby lcl » Mon Mar 10, 2014 5:17 pm

skydereign wrote:It doesn't happen for me. The code runs fine, and from a cursory glance at it, it doesn't look like it should cause any problems. One thing though is you should free the memory when you are done with it (so when you exit the game), which might be causing some problems.

Yes, it sometimes happened to me just after a few times of entering Game Mode and then returning to the editor.
But it turned out that it was the last line in function drawPolygon() causing the problem. I commented out the free() and it worked stable.
(And I noticed that it was due to me freeing the node * pointer, while I probably should've freed node * first because node * pointer wasn't pointing to the beginning of the allocated area anymore.)

skydereign wrote:
lcl wrote: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.

To clarify you are using a pointer to an array of structs, not an array of pointers to structs. An array of pointers to structs would be node* []. So technically what you have is an array of structs. Do you mean you don't want the pointer at all? If so then you can do that, it just won't be dynamic in size and you have to create it in advance.

When saying an array of pointers to structs, I referred to another version of the code, which worked fine.
In that version I had a node ** nc which I first allocated as malloc(cornerNumber * sizeof(node *)) and then in a for loop I did nc[i] = malloc(cornerNumber * sizeof(node)), so wouldn't that be actually an array of pointers to an array of structs?

I'm a little confused with this whole memory allocation thing still, and I really am not sure when I should call the allocated thing a pointer and when I should call it an array..

And umm.. sorry to bother you this much, but I have some questions concerning the correct way of freeing the memory used.
If I allocate the memory in the way I do in the function buildPolygon, and pass that functions return value to drawPolygon, is it correct to free the memory just by having a free(first) on the last line of the function drawPolygon()?

Also, if I use the function buildTriangle and pass that function's return value to drawPolygon, is it correct to free the memory by calling freeTriangle(first)?
Code: Select all
node * buildTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
    node * c1;
    node * c2;
    node * c3;
 
    c1 = malloc(sizeof(node));
    c2 = malloc(sizeof(node));
    c3 = malloc(sizeof(node));
 
    if (c1 && c2 && c3)
    {
        c1->x = x1;
        c1->y = y1;
        c1->next = c2;

        c2->x = x2;
        c2->y = y2;
        c2->next = c3;
 
        c3->x = x3;
        c3->y = y3;
        c3->next = NULL;
    }
 
    return c1;
}

void freeTriangle(node * pointer)
{
    free(pointer->next->next);
    free(pointer->next);
    free(pointer);
}

Really, I'm sorry for bothering you so much, but I just want to be sure that I'm freeing the memory correctly, and it's impossible to know that without asking.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Dynamically allocated array of structs

Postby bat78 » Tue Mar 11, 2014 6:24 am

Hello lcl,
Malloc, calloc, relloc and free (memset-like) functions are complicated.
When you run your game, open in a background window task manager and spectate the process's memory usage. If it goes higher, you have a problem with the freeing.. and in most cases, it won't work if not allocated and it will crash if not allocated correctly.

Since i don't have much time to test your code (thanks to @skydreign it is already tested) but i can give you some examples that may clean out some things.

Code: Select all
int l = 0;

struct data
{
    int R;
    int G;
    int B;
};

struct data *array[64]; //declaring the array

for (l=0; l<64; l++)
{
    array[l] = malloc(sizeof(struct data)); //allocating the array
}
//now we gotta declare pointer to the array

struct data *(*p)[] = &array;


And this would allow you to do:
Code: Select all
(*p)[30]->R=255;


What are you trying to do may be a bit more complicated then i can take xD You wan't dynamic pointer array of single-dynamic allocated structs.
In this case you gotta allocate it like:

Code: Select all
struct data *p = malloc(N*sizeof(*p));
struct data **pp = malloc(N*sizeof(*pp));
//(n - num)

then just free with `free(p), free(pp);` but don't forget to loop through all the slots of p and pp. Good luck!

bat78
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: Dynamically allocated array of structs

Postby Zivouhr » Sat Jan 16, 2016 5:25 pm

I noticed lcl said game editor would crash after exiting game mode. Though I'm not using structs (or arrays) in my game, every fourth time exiting out of game mode while in the editor, game editor crashes, and sometimes says out of memory. Thankfully in the exported game, this doesn't happen. Even so, once a game gets very large with lots of code, I noticed this happens more often, but not sure why other than limited RAM or memory for the computer.
City of Rott Game created on Game Editor http://cityofrott.wordpress.com/
User avatar
Zivouhr
 
Posts: 549
Joined: Sat May 17, 2014 2:12 pm
Score: 59 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron