I have an array of lists that i want to double in size.I use realloc and when i start to populate the new array elements the starting elements start becoming NULL and i can't change their value.
int x;
Heads=realloc(Heads, TABLESIZE * sizeof(struct HTLink));
S=realloc(S, TABLESIZE * sizeof(HTHash));
for(x=TABLESIZE;x<TABLESIZE*2;x++)
{
S[x]=(HTNode *)malloc(sizeof(HTNode));
S[x]->Key=EmptyKey;
S[x]->Next=NULL;
Heads[x]=*S[x];
}
TABLESIZE*=2;
return S;
After i run this code the values of S[2] and upward are normal but S[0] and S[1] are NULL
You are confused on what the second argument of realloc
is:
void* realloc (void* ptr, size_t size);
size
: New size for the memory block, in bytes.
You are doing this:
S=realloc(S, TABLESIZE * sizeof(HTHash));
for(x = TABLESIZE; x < TABLESIZE * 2; x++)
which shows that you think that realloc()
will expand the array's size by the second parameter. Change it to:
S=realloc(S, 2 * TABLESIZE * sizeof(HTHash));