Hi,
I'm trying to get a dynamic array working inside my program. I'm new to DMA and so am not up to scratch with it - i've just been plugging knowledge in from everywhere.
Anyway, i've got the arrays working so i can add new entries as and when i want. BUT, the data seems to get corrupted at random points by realloc statements. Can anyone explain this to me?
Heres my code:
// Get space for dynamic arrays
freq = (int *)malloc(size*sizeof(int)+1);
num = (int *)malloc(size*sizeof(int)+1);
num[1] = *list[0];
freq[1]=1;
// Go through price list
for(count=1;count<=days-1;count++)
{
// Note frequency of entries
match=0;
for(c=1;c<=days;c++){if(*list[count]==num[c]){freq[c]++;match=1;break;}}
if(match==0){
size++;
realloc(freq,size*sizeof(int)+1);
freq[size] = 1;
realloc(num,size*sizeof(int)+1);
num[size] = *list[count];
}
}
Cheers in advance
I'm trying to get a dynamic array working inside my program. I'm new to DMA and so am not up to scratch with it - i've just been plugging knowledge in from everywhere.
Anyway, i've got the arrays working so i can add new entries as and when i want. BUT, the data seems to get corrupted at random points by realloc statements. Can anyone explain this to me?
Heres my code:
// Get space for dynamic arrays
freq = (int *)malloc(size*sizeof(int)+1);
num = (int *)malloc(size*sizeof(int)+1);
num[1] = *list[0];
freq[1]=1;
// Go through price list
for(count=1;count<=days-1;count++)
{
// Note frequency of entries
match=0;
for(c=1;c<=days;c++){if(*list[count]==num[c]){freq[c]++;match=1;break;}}
if(match==0){
size++;
realloc(freq,size*sizeof(int)+1);
freq[size] = 1;
realloc(num,size*sizeof(int)+1);
num[size] = *list[count];
}
}
Cheers in advance