once again clone trouble! help!

Non-platform specific questions.

once again clone trouble! help!

Postby sonicfire » Wed Sep 07, 2011 10:03 am

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? :)
sonicfire
 
Posts: 425
Joined: Wed Nov 01, 2006 9:34 pm
Location: berlin germany
Score: 16 Give a positive score

Re: once again clone trouble! help!

Postby sonicfire » Wed Sep 07, 2011 10:06 am

Hooray! Made it work myself:

Code: Select all
char tma[255];


...that´s better :)
sonicfire
 
Posts: 425
Joined: Wed Nov 01, 2006 9:34 pm
Location: berlin germany
Score: 16 Give a positive score

Re: once again clone trouble! help!

Postby skydereign » Wed Sep 07, 2011 10:27 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: once again clone trouble! help!

Postby lcl » Wed Sep 07, 2011 12:25 pm

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? :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: once again clone trouble! help!

Postby sonicfire » Wed Sep 07, 2011 1:14 pm

Thanks, Sky :) There´s still a lot to learn
sonicfire
 
Posts: 425
Joined: Wed Nov 01, 2006 9:34 pm
Location: berlin germany
Score: 16 Give a positive score

Re: once again clone trouble! help!

Postby skydereign » Wed Sep 07, 2011 10:51 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: once again clone trouble! help!

Postby sonicfire » Thu Sep 08, 2011 8:01 am

Thanks for clarifying this :)
sonicfire
 
Posts: 425
Joined: Wed Nov 01, 2006 9:34 pm
Location: berlin germany
Score: 16 Give a positive score

Re: once again clone trouble! help!

Postby lcl » Thu Sep 08, 2011 9:15 am

Thanks sky! :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest