I am having trouble with some code in C and i would really need your help . Well i have these 2 structs (they are asked to be like this so we cann't change them)
struct node{
struct list_node **table;
int table_size;
};
struct list_node{
int num;
struct list_node *ptr;
};
and as you can see in the first one we have an array , with pointers in the nodes of our list. in main , we create the needed memory space to begin like this
struct node *nodeT;
struct list_node *root, *curr, **temp;
root = (struct list_node*)malloc(sizeof(struct list_node)); // root node of list
nodeT = (struct node*)malloc(sizeof(struct node)); // single node
nodeT->table = (struct list_node**)malloc(sizeof(struct list_node*));
nodeT->table_size = 0;
and then I creat the list
for(i=0 ; i<X ; i++){ // X is important for the errors and i'll explain why
curr = (struct list_node*)malloc(sizeof(struct list_node));
curr -> num = (i+1);
curr -> ptr = root;
root = curr;
}
now , i run through the list and i expand the array in the first struct for everysingle list node i find , to enter a pointer to the proper node.
for(curr=root ; curr!=NULL ; curr=curr->ptr){
nodeT->table[nodeT->table_size] = curr;
nodeT->table_size++;
temp = (struct list_node**)realloc(nodeT->table , nodeT->table_size*sizeof(struct list_node *));
if(temp!=NULL)
nodeT->table = temp;
else{
printf("Memory error\n");
return 1;
}
}
i use this struct list_node **temp to keep safe nodeT and after checking , if everything is ok , i put temp in nodeT again , otherwise i stop the programm . In the end i print the contents of the list throught the pointers of array like this
for(i=0 ; i<nodeT->table_size ; i++)
printf("-> %d ", nodeT->table[i]->num);
printf("\n");
and i exit the programm. the paradox in this , is that for X 1-4 everything works fine, but for 5+ there is a problem and i get a message
" ** glibc detected * ./dynamix_table_realloc: realloc(): invalid next size: 0x0000000000819050 * "
and about 20 more lines , that dont realy help me. I hope you will , and thats why i posted this. Thanks in advance!