Thread: Lists of lists: last element of list overwritten by first element of another

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    29

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > free(test_token);
    Yes, why do you do this?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    29
    That was it - Thank you very much Salem!

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    while (fgets(line, sizeof(line), obstaclePositions_traj_file) )
    fgets() will read at most one '\n', which will be the last character in 'line' if it exists.

    Code:
    token = strtok(line, "\n"); //Pull the string apart into tokens using the \n
    Since there is only one '\n' in 'line' and it is at the end, strtok() will as a side effect change it to a '\0'. I.e. this line just removes the '\n' at the end of the 'line' using a rather unusual way.

    Code:
    for( token = strtok(line, "\t"); token ; token = strtok (NULL, "\t") )
    The initialization part of the for-loop throws away the result from the previous strtok()-call, i.e. the previous call is completely unnecessary if you use strtok() with the delimiter "\t\n" in your for-loop. This will split the string into tokens separated by either '\t' or '\n'.

    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);
    It looks like you are accessing your lists using indices which is not very efficient. Why don't you use a simple loop which advances a pointer from one element to the next using the pointer to the next element?

    Code:
    coords = (char*)op_prg_list_access (line_coord_list, j);
    printf("%c", coords);
    Don't you get a warning when you compile? 'coords' seems to be a char pointer, but '%c' expects a char.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. List of lists - memory overwritten (I think)
    By kerrymaid in forum C Programming
    Replies: 1
    Last Post: 02-15-2013, 11:50 PM
  2. Replies: 1
    Last Post: 02-07-2013, 10:35 AM
  3. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  4. list first element
    By wyrwidab in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2005, 11:42 AM
  5. Replies: 22
    Last Post: 10-22-2005, 08:42 AM