Hi all,
I use a network simulator based around C. In one of my C files I have a global linked list (called obstacle_list saved in a header file) which contains smaller lists (not global). In another C file at another point I try to access this global linked list. I can see it hold 380 items but when I try to access the inner linked lists, they’re empty.
It must be a memory allocation oversight when I am populating the lists first day (as I read them back at the time to check and all seems fine).
Does anyone have any advice based on the below code?
Many thanks.
Code:
fgets(line, sizeof(line), obstaclePositions_traj_file);
obstacle_list = op_prg_list_create();
tg = 0;
while ((line != OPC_NIL) && (chk <380))
{
//read the first \t into id, read the remaining into a global obstacle list
token = strtok(line, "\t\n"); //Pull the string apart into tokens using the commas
input = op_prg_list_create();
while (token != NULL)
{
test_token = strdup(token);
if (op_prg_list_size(input) == 0)
op_prg_list_insert(input,test_token,OPC_LISTPOS_HEAD);
else
op_prg_list_insert(input,test_token,OPC_LISTPOS_TAIL);
token = strtok (NULL, "\t\n");
}
if (op_prg_list_size(obstacle_list) == 0)
op_prg_list_insert(obstacle_list,input,OPC_LISTPOS_HEAD);
else
op_prg_list_insert(obstacle_list,input,OPC_LISTPOS_TAIL);
chk = chk + 1;
if (chk == 375)
printf ("check here");
if (chk <= 379)
fgets(line, sizeof(line), obstaclePositions_traj_file);
}
//check the list has been populated correctly below
/*size_ob_list = op_prg_list_size (obstacle_list);
for (k = 0; k <size_ob_list; k++)
{
line_coord_list = (List*)op_prg_list_access (obstacle_list, k);
count_inner_list = op_prg_list_size (line_coord_list);
for (j=0; j< count_inner_list; j++)
{
coords = (char*)op_prg_list_access (line_coord_list, j);
printf("%c", coords);
}
}*/
FOUT;
}