Page 1 of 1

once again clone trouble! help!

PostPosted: Wed Sep 07, 2011 10:03 am
by sonicfire
I have 20 black actors / clones on screen, created via script.
Now i wanna remove them like this (timer runs 19 times):
Code: Select all
char *tma;
sprintf(tma, "black.%i", no);
no++;
DestroyActor(tma);


it just crashes GE. what did i wrong?
Why doesn´t this work? :)

Re: once again clone trouble! help!

PostPosted: Wed Sep 07, 2011 10:06 am
by sonicfire
Hooray! Made it work myself:

Code: Select all
char tma[255];


...that´s better :)

Re: once again clone trouble! help!

PostPosted: Wed Sep 07, 2011 10:27 am
by skydereign
Yeah, the problem with using a char* like that is that you haven't allocated memory for it. So essentially, you created a pointer that doesn't point to anything. As you found out, gE reacts when you try to write to it by crashing. In future if you want to use a char* instead of a char[], you can use this.
Code: Select all
char* tma = malloc(255);

char[]s already have memory allocated for them, while pointers only have an address.

Re: once again clone trouble! help!

PostPosted: Wed Sep 07, 2011 12:25 pm
by lcl
skydereign wrote:Yeah, the problem with using a char* like that is that you haven't allocated memory for it. So essentially, you created a pointer that doesn't point to anything. As you found out, gE reacts when you try to write to it by crashing. In future if you want to use a char* instead of a char[], you can use this.
Code: Select all
char* tma = malloc(255);

char[]s already have memory allocated for them, while pointers only have an address.

Thanks for useful info once again, skydereign! :D
How come char * can still be used in function created at global code with out using malloc(); ?
Like this:
Code: Select all
void Jump(char * name, int number, int power)
{
    Actor * actor = getclone(name);
    if (jump[number] == 0)
    {
        actor->yvelocity = - power;
        jump[number] = 1;
    }
}

(Some my very old function for jumping.. :P)
That code works perfectly without crashing.

And what is the difference between using char myString[255] and char * myString?
I mean, what are these two for? :)

Re: once again clone trouble! help!

PostPosted: Wed Sep 07, 2011 1:14 pm
by sonicfire
Thanks, Sky :) There´s still a lot to learn

Re: once again clone trouble! help!

PostPosted: Wed Sep 07, 2011 10:51 pm
by skydereign
Well when you use (char* string) that is a pointer to a string. So, "constant strings like this", have a place in memory, so they are already allocated. For instance, you can use a char* like this.
Code: Select all
char* string = "0000000000";

That will allocate enough memory for the string, but it is easier, and generally better to allocate with malloc (you can also increase the length of the string). Back to the point, the string you actually pass into the function is the string that the pointer is pointing to, whether it be a constant char * (so a string in quotes), or a char[], or even another char*. Now, in gE, there isn't too much reason to use a char* over a char[], but char*s are more versatile. Pointers though allow C to be quicker, as you are dealing with addresses, which makes it that more memory efficient, but again unless you are dealing with large structs, or similar, that isn't really an issue.

Re: once again clone trouble! help!

PostPosted: Thu Sep 08, 2011 8:01 am
by sonicfire
Thanks for clarifying this :)

Re: once again clone trouble! help!

PostPosted: Thu Sep 08, 2011 9:15 am
by lcl
Thanks sky! :D