Error in ge

Non-platform specific questions.

Error in ge

Postby SuperSonic » Fri Feb 24, 2012 12:04 am

Hi guys :D

Ok, so in my global code, I've got this:
Code: Select all
int **level;

And then I've got a draw actor event that looks like this:
Code: Select all
char *key = GetKeyState();

if(key[49])
{
    level[xmouse][ymouse] = 1;
}

The error I get is:
Ge Error wrote:Draw actor: READ error in invalid memory area

Anyone know how to solve this? :roll:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Error in ge

Postby skydereign » Fri Feb 24, 2012 12:11 am

The problem is because you are trying to access uninitialized memory. It's the same problem trying to use a char* before you set it to something.
Code: Select all
int** level; // this doesn't actually point to anything
int level2[10][10]; // this points to a 2d int array


So you can either allocate the memory with C's memory allocators, or the recommended way, give the array a size.

-Edit
And actually looking at that code you shouldn't do that. xmouse and ymouse are not integer values. You should at least truncate them to be integers. And since you are using that, you know what the max array size is, in your case [view.width][view.height] though you should replace the variables with their literal counterparts.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Error in ge

Postby SuperSonic » Fri Feb 24, 2012 12:22 am

Ok thank you :D

But here's the thing. I need to be able to have the size unlimited. So could you explain how to use the memory allocators please? :D
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Error in ge

Postby skydereign » Fri Feb 24, 2012 12:39 am

It'll never be truly unlimited, and from the sounds of it you don't need it to be. But to allocate memory you can use the malloc/calloc/realloc functions.
Code: Select all
char* string = malloc(255);
// normally string would not point to anything but in this case we are setting string to point to the output of malloc
// malloc returns a chunk of memory size determined by the input
// so currently that char* is the same size as the following
char string2[255];

calloc operates a little differently. It takes a data type size and a number. The size of the type is the size of an int, char, or whatever you are allocating memory for. The number determines the amount you should allocate for.
Code: Select all
char* string = calloc(sizeof(char), 255);
// same as the above lines of code


Now in your case you want to allocate based off some large width and height. You need to allocate each dimension separately (first the height, and then the width).
Code: Select all
int ** level = malloc(sizeof(int)*level_height);
int i;
for(i=0;i<level_height;i++)
{
    level[i]=malloc(sizeof(int)*level_width);
}


Notice though this is just the same thing as predetermining the width and height of the array (but it now has the problem of needing to be freed). One of the benefits to allocating your own memory dynamically is that if you can allocate only the space you need (instead of guessing and allocating a lot more than you need). The other one though is you can use realloc, which allows you to change the size of the pointer you passed it. So, you can change the size of a dynamically allocated array. To use realloc you pass it the pointer you want to change the size of and the new size. If you allocate more space then it will store the contents. I got to go so I'll wrap this up with the following warning. Memory that is allocated dynamically must be freed, otherwise you have a memory leak. So if you use dynamic allocation in a function, you'll have to remember to call free on it.
Code: Select all
void
waste_memory ()
{
    char* test = malloc(255);
    // doesn't free the memory and since the space was not passed out of the function it is lost
}

void
useless ()
{
    char* test = malloc(255);
    free(test); // frees the memory and therefore no memory leak
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Error in ge

Postby SuperSonic » Fri Feb 24, 2012 12:43 am

Awsome thanks. I'm gonna study this :D

And I didn't mean to say "unlimited". I meant that I need it to be bigger than just 256. And I thought that Ge didn't let you create arrays bigger than 256. I dunno but thanks anyways :P
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Error in ge

Postby skydereign » Fri Feb 24, 2012 12:47 am

You can create potentially much larger arrays (you are thinking about strings). gE's built in strings are size 256, and that means you will have trouble if trying to display an array larger than 256 via text. So you can declare arrays a lot larger as long as you aren't dealing with gE's built in arrays. But, I think Bee-Ant has mentioned problems with too large arrays, so do watch out for that.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Error in ge

Postby SuperSonic » Fri Feb 24, 2012 1:13 am

Like what problems? :)
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Error in ge

Postby SuperSonic » Thu Mar 08, 2012 1:05 am

Sorry to bump this up :roll:

I have three questions. First, could you please explain exactly what free() does? I kinda get the basic concept but I would rather know all of it^^.Second, how would I use realloc() to change the size of a 2d array? :D

And third:
skydereign wrote:But, I think Bee-Ant has mentioned problems with too large arrays, so do watch out for that.
I wrote:Like what problems? :)

:P
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Error in ge

Postby skydereign » Thu Mar 08, 2012 1:52 am

SuperSonic wrote:I have three questions. First, could you please explain exactly what free() does? I kinda get the basic concept but I would rather know all of it^^.

free frees up allocated memory. So you know how malloc/calloc allocates memory for the user? What it essentially does is reserve a certain amount of space. Now if you keep reserving more and more space, eventually you'll run out of space to use. So, when you are done with the memory (no need for it anymore) you should free it. If you don't, it's a memory leak, and can cause problems for you. C handles things via pointers/addresses. A char* for instance is a pointer to a char in memory. So, when you allocate a block of memory, you need to have away of pointing to the memory (otherwise you have a memory leak).
Code: Select all
char* string = malloc(255);

malloc in that case returns the address of a block of memory (size 255). Now free takes an address and frees the memory it points to. So if you used free(string) it would free the memory that string points to. That means that string now points to memory it shouldn't, so you should set it to point to NULL.
Code: Select all
free(string);
string=NULL;

SuperSonic wrote:Second, how would I use realloc() to change the size of a 2d array? :D

I actually posted something you shouldn't do in my previous explanation. To allocate memory for a 2d int array, what you really are doing is allocating an array of int pointers, and then allocating each of those as int arrays.
Code: Select all
int i;
int a_w = 10;
int a_h = 10;
int ** p;

p = malloc(sizeof(int*)*a_h);
for(i=0;i<a_h;i++)
{
    p[i] = malloc(sizeof(int)*a_w); // allocates each sub array individually
}

The second dimension makes allocating/reallocating/freeing the array a lot more complex. To free the above, you must first free the p[i] arrays, and then free the p array. And to reallocate you must reallocate each p[i] array individually as well as reallocate the p array. This can easily get overwhelming. So, really I would recommend allocating a single dimensioned array, and treat it like a two dimensional array. Then whenever you need to reallocate the array, you allocate a new one, copy over the old values into the new one, and then switch them. Still a bit of work, but overall cleaner than dealing with a 2d array.

SuperSonic wrote:And third:
skydereign wrote:But, I think Bee-Ant has mentioned problems with too large arrays, so do watch out for that.
I wrote:Like what problems? :)

:P

Oh yeah. I searched for it but didn't find the actual problem. Though I believe it had to do with crashing the game (could be wrong).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Error in ge

Postby SuperSonic » Thu Mar 08, 2012 2:54 am

Ok thank you and +1 :wink:
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest