Page 1 of 1

Would this work? (array resetting)

PostPosted: Sun Apr 29, 2012 1:57 am
by Hblade
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);

Re: Would this work? (array resetting)

PostPosted: Sun Apr 29, 2012 4:35 am
by skydereign
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*.

Re: Would this work? (array resetting)

PostPosted: Sun Apr 29, 2012 1:13 pm
by SuperSonic
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

Re: Would this work? (array resetting)

PostPosted: Sun Apr 29, 2012 4:11 pm
by skydereign
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.

Re: Would this work? (array resetting)

PostPosted: Sun Apr 29, 2012 8:44 pm
by SuperSonic
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

Re: Would this work? (array resetting)

PostPosted: Sun Apr 29, 2012 8:48 pm
by skydereign
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.

Re: Would this work? (array resetting)

PostPosted: Sun Apr 29, 2012 11:14 pm
by Hblade
oh :) Thanks guys

Re: Would this work? (array resetting)

PostPosted: Mon Apr 30, 2012 1:36 am
by SuperSonic
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? :)

Re: Would this work? (array resetting)

PostPosted: Mon Apr 30, 2012 2:18 am
by skydereign
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.

Re: Would this work? (array resetting)

PostPosted: Mon Apr 30, 2012 3:09 am
by SuperSonic
But I thought that calloc wouldn't work with a pre-defined array. For example: int i[2] = calloc(2);

Would that work?

Re: Would this work? (array resetting)

PostPosted: Mon Apr 30, 2012 4:26 am
by skydereign
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.

Re: Would this work? (array resetting)

PostPosted: Mon Apr 30, 2012 10:21 pm
by SuperSonic
Ok, thank you for clearing that up :)