Thread: Segfault when removing element from list

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532

    Segfault when removing element from list

    It's been a while, haven't used lists much, but I'm trying to remove a specific element from a list I have.
    I iterate through the list until my iterator is pointing to the correct element, then I call the erase member function, passing my iterator as an argument to the erase function. When that piece of code runs, it causes a segfault. Here's what it looks like:
    Code:
    for(iter=openlist.begin();iter!=openlist.end();iter++)
    		{
    			temp=iter->get_pos();
    			if(disp.x==temp.x&&disp.y==temp.y)
    			openlist.erase(iter);
    		}
    The temp and disp variables are structs that just contain two integers(x and y). The openlist is my list(obviously), and I want to remove the element in openlist that has the same coordinates as the set found in disp. The list contains a class I made. I'm wondering if there's a better, easier way to remove that specific element with those coordinates(and yes, the coordinates appear only once)? Or is there a reason that's giving me a segmentation fault? Any help or pointers are appreciated, thanks.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you are incrementing a deleted iterator.
    If there is only one element that you want to remove then a quick fix would be
    Code:
                    for(iter=openlist.begin();iter!=openlist.end();iter++)
    		{
    			temp=iter->get_pos();
    			if(disp.x==temp.x&&disp.y==temp.y) {
    			    openlist.erase(iter);
                                break;
                            }
    		}
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Will you ever erase more than one element from the list? If not, then you should break the loop immediately after erasing.

    If it is possible to erase more than one element, then you need to modify your loop. Calling erase invalidates the iterator it erases. You then increment that iterator and compare it to the end() iterator. This is illegal. The common way to erase from an stl sequence container like this is:
    Code:
    iter=openlist.begin();
    while (iter!=openlist.end())
    {
    	temp=iter->get_pos();
    	if(disp.x==temp.x&&disp.y==temp.y)
    		iter = openlist.erase(iter);
    	else
    		++iter;
    }

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Thank you both. That was exactly my problem. It didn't dawn on me that such was taking place. Thanks again, problem solved.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A good way to erase elements from a list based on some condition, is the following:
    Code:
    list.remove_if(RemovePredicate);
    where RemovePredicate is a function that takes a single parameter with the same type as the list is holding (the current value) and returns true if the element should be removed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  2. delete an element from a linked list
    By hinman in forum C Programming
    Replies: 6
    Last Post: 10-17-2007, 08:30 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM