Yes I forgot that realloc returns a new pointer sometimes :x
Yes I can use malloc and it will work as intended, BUT everytime numeffectids changes it will allocate new memory instead of reallocate the old memory to the right size.
As I couldn't get it to work I will use a workaround for now using two sole arrays. In real code I will do the error handling and I will use a temp pointer as the return value of realloc so if it failes, my old pointer is not gone.
Code:
int k;
int numeffectids = 0, oldnumeffectids = numeffectids;
unsigned int *effectids_val, *effectids_time;
effectids_val = malloc(0);
effectids_time = malloc(0);
while(1)
{
if(numeffectids == 0) goto L_End;
effectids_val = realloc(effectids_val, sizeof(int)*numeffectids);
if(effectids_val == NULL)
printf("Out of memory!1\n");
effectids_time = realloc(effectids_time, sizeof(int)*numeffectids);
if(effectids_time == NULL)
printf("Out of memory!2\n");
else if(numeffectids > oldnumeffectids)
{
effectids_val[oldnumeffectids] = numeffectids;
effectids_time[oldnumeffectids] = GetTickCount();
printf("%u - %u\n", effectids_val[oldnumeffectids], effectids_time[oldnumeffectids]);
}
// else printf("new: %d, old: %d\n", numeffectids, oldnumeffectids);
L_End:
oldnumeffectids = numeffectids;
rand()%2==0?numeffectids++:numeffectids--;
if(numeffectids < 0)
numeffectids = 0;
Sleep(10);
}
I was already familiar with allocating memory space but I couldn't program much in the last time and I forgot some stuff =/
But how would I convert this piece of code to one that uses one 2d array instead of two arrays?