Would this work? (array resetting)

Non-platform specific questions.

Would this work? (array resetting)

Postby Hblade » Sun Apr 29, 2012 1:57 am

Code: Select all
int globalVars[9999];
void ResetArray(char *arrayName, int arrayAmmount)
{
    int i;
    for(i=0;i<arrayAmmount-1;i++)
    {
        (int)arrayName[i]=0;
    }
}


How would I make this work? i want to be able to do something like this:
Code: Select all
ResetArray("globalVars", 9999);
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Would this work? (array resetting)

Postby skydereign » Sun Apr 29, 2012 4:35 am

Why would you want to use strings for that? So the player/user can use strings? I wouldn't recommend it, as you can easily do this.
Code: Select all
ResetArray(globalVars, 9999);

And the function just takes an int*.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Would this work? (array resetting)

Postby SuperSonic » Sun Apr 29, 2012 1:13 pm

Or a long int* :)

But anyways, I thought there was a way to create "templates" for every data type. Or is that only possible in c++? :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: Would this work? (array resetting)

Postby skydereign » Sun Apr 29, 2012 4:11 pm

You can use a void* if you want. The problem is if you are using a void* it would be better to use memset to 0 it (which doesn't work properly in gE). But as long as you keep it to simple data types, a void* would work with no casting problems.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Would this work? (array resetting)

Postby SuperSonic » Sun Apr 29, 2012 8:44 pm

Or perhaps you could just use malloc(int) since it returns an empty space anyways. Wouldn't that be the equivilant of resseting an array? :)
EG:
Code: Select all
int myarray[5];
myarray[0] = 10;
myarray = malloc(5);//Resets
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: Would this work? (array resetting)

Postby skydereign » Sun Apr 29, 2012 8:48 pm

No, you definitely don't want to do that. malloc returns an address to new memory. Doing that would cause a memory leak (assuming that even worked, which it doesn't), and it wouldn't even return 0'd memory. malloc just returns the address to a chunk of memory (regardless of what was there). If you wanted it to be 0, you would use calloc. And as I mentioned, that wouldn't work with an int[]. That would have to be an int*, that way dynamic memory allocation works.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Would this work? (array resetting)

Postby Hblade » Sun Apr 29, 2012 11:14 pm

oh :) Thanks guys
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Would this work? (array resetting)

Postby SuperSonic » Mon Apr 30, 2012 1:36 am

skydereign wrote:malloc returns an address to new memory. Doing that would cause a memory leak (assuming that even worked, which it doesn't), and it wouldn't even return 0'd memory. malloc just returns the address to a chunk of memory (regardless of what was there)
Oh yeeeeah... :|
skydereign wrote:If you wanted it to be 0, you would use calloc
Arg! I keep forgetting about that. I'm so used to using malloc ^^
So is there any way to create a function that takes a parameter and returns an empty array that is the size of the parameter? :)
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: Would this work? (array resetting)

Postby skydereign » Mon Apr 30, 2012 2:18 am

SuperSonic wrote:So is there any way to create a function that takes a parameter and returns an empty array that is the size of the parameter? :)

That is pretty much what calloc and malloc do. You call it to get a chunk of memory, and then cast it to the parameter.
Code: Select all
int* a1 = (int*)calloc(10, sizeof(int));
char* a1 = (char*)calloc(10, sizeof(char));

The calloc function returns a void* to the memory, and all you do is cast it into the pointer. You could write a function that casts it for you, but that would be more complicated than using calloc and malloc.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Would this work? (array resetting)

Postby SuperSonic » Mon Apr 30, 2012 3:09 am

But I thought that calloc wouldn't work with a pre-defined array. For example: int i[2] = calloc(2);

Would that work?
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: Would this work? (array resetting)

Postby skydereign » Mon Apr 30, 2012 4:26 am

No, dynamic memory allocation is only for pointers. int var[10] declares a statically sized array. The location of the memory it uses is different that dynamically allocated memory. You only allocate memory if you are dealing with something that doesn't have memory allocated for it, right? In the case of arrays declared with the brackets, you already have memory set aside for it. Also, when setting an array equal to something, you have to use the brackets.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Would this work? (array resetting)

Postby SuperSonic » Mon Apr 30, 2012 10:21 pm

Ok, thank you for clearing that up :)
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