I have a structs like so
typedef struct ll_node {
uint data;
struct ll_node* next;
} ll_node;
typedef struct {
ll_node* head;
uint count;
} linked_list;
typedef struct {
linked_list* lists;
uint list_size
} Context;
And I need to be able to have lists grow dynamically from context. Here is how I'm adding a linked list to my lists array
void add_list(Context* c) {
if (c->list_size == 0) {
c->lists = malloc(sizeof(linked_list));
} else {
realloc(c->lists, sizeof(linked_list) * (c->list_size + 1));
}
linked_list_init(c->lists[c->list_size]);
list_size++;
}
The problem comes in whenever I realloc. I lose the data that is not the most current index in lists. So if c->lists[0] contained 1==>1==>2 then it would be gone and c->lists[1] would be initialized and ready for linked list functions.
Any help would be greatly appreciated