1. ## Dynamic Array

Hi. I'm working with dynamic arrays to construct a histogram. I've got an array of pointers to my histogram struct and I try to malloc() and then realloc() the array as necessary. Here's some of the code:
Code:
```   typedef struct{
double val;
int freq;
}hPoint;
...
...
...

hPoint **momentBin;
momentBin = (void *)malloc(sizeof(hPoint *) * 10);int maxIndex =10;

...

found=0;
for(k=0;k<maxIndex;k++)
{
if(momentBin[k]==NULL)
break;
test =(momentBin[k])->val;
if (test==bN)
{
found =1;
(momentBin[k])->freq++;
break;
};

}
if(found ==0)
{
probCounter++;
//increase the size of the array
if (probCounter>=maxIndex)
{
hPoint **newArr = realloc(momentBin, maxIndex*2);
if (newArr==NULL)
{
printf("Reallocation Error");exit(0);
}
else momentBin = newArr;
};
hPoint *temp =(hPoint *) malloc(sizeof(hPoint));
temp->val=bN;
temp->freq=1;
momentBin[probCounter] = temp;```
This would be great, except when I loop through my array, there aren't any structs. Your help is greatly appreciated.
Thanks

2. What do you mean by "there aren't any structs"?

Also: do you change maxIndex after you realloc?

3. I have a looping statement to read through all of the array:
Code:
```for(j=0;j<maxIndex;j++)
{
if(momentBin[j]==NULL)
break;
test =(momentBin[j])->val;
printf("%lf\n",test);
}
printf("Last J: %d",j);```
I changed the max index thing to update, but when I run, it still reads j=0

4. Originally Posted by firetheGlazer
I have a looping statement to read through all of the array:
Code:
```for(j=0;j<maxIndex;j++)
{
if(momentBin[j]==NULL)
break;
test =(momentBin[j])->val;
printf("%lf\n",test);
}
printf("Last J: %d",j);```
I changed the max index thing to update, but when I run, it still reads j=0
Just because you didn't put anything in 0, doesn't mean you didn't put anything in 1, or 4, or 11, or .... IOW: use continue instead of break.

5. Wow, okay thanks for the catch. When I put in a continue, GDB spit out my assignment line with a segfault:
Code:
```Program received signal SIGSEGV, Segmentation fault.
0x00401758 in main () at Z:\VortexStuff\Moment\moment.c:169
169	         test =(momentBin[j])->val;```
I thought that my null test would have caught that. Any thoughts?