Lists of lists: last element of list overwritten by first element of another
Hi,
I have a global list that contains smaller lists of char arrays. I have an issue where when I’m reading back the inner lists the last element of one list seems to point to first element of the next.
So my data looks like the below (values separated by commas with the pairs separated by tabs. The last pair in a line is the same as the first). When I read the first list back instead of seeing “456.678,678.98” as the last element in the list. I see “435.67,234.98” twice: at the end of the first list and start of the other. I have debugged when the list is populated and can see the correct values going in so I can’t figure what’s happening.
456.678,678.98 123.45,345.56 256.67,789.98 456.678,678.98
435.67,234.98 123.65,342.56 987.78,678.34 435.67,234.98
Code to fill the list:
Code:
obstacle_list = op_prg_list_create();
while (fgets(line, sizeof(line), obstaclePositions_traj_file) )
{
token = strtok(line, "\n"); //Pull the string apart into tokens using the \n
input = op_prg_list_create();
for( token = strtok(line, "\t"); token ; token = strtok (NULL, "\t") )
{
test_token = strdup(token);
op_prg_list_insert(input, test_token, OPC_LISTPOS_TAIL);
}
op_prg_list_insert(obstacle_list,input, OPC_LISTPOS_TAIL);
free(test_token);
}
Code to read back the list:
Code:
size_ob_list = op_prg_list_size (obstacle_list);
line_coord_list = op_prg_list_create();
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);
}
}
Any advice appreciated.