Thread: vector::pop_back & erase problems

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    vector::pop_back & erase problems

    Hi,

    I use a vector to hold connection information for each client in my piece of server sofware.

    I'm having a problem when removing elements from my vector when clients disconnect.

    When I only have one client connected and therefore one element in the vector, is when the problem is most obvious. Everything is fine until I use either vector.pop_back or vector.erase to remove the element (when the client disconnects). Then my program throws what I think is an execption.

    This only happens for the last element in the vector. For example, if two clients connect, I can easily remove the element for the first client without a problem. Then, if I attempt to remove the second (which is now the only element), I get an exception.

    If you need specific code snippets, please don't hesitate to ask. I just thought this might be a known issue with vectors.

    Any help greatly appreciated. Many thanks,

    Daniel Briley
    Last edited by codec; 03-30-2004 at 10:51 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If you need specific code snippets, please don't hesitate to ask.
    Give us a small, compilable program that exhibits the problem.

    >I just thought this might be a known issue with vectors.
    Not to my knowledge. Chances are that you are just doing it wrong.
    My best code is written with the delete key.

  3. #3
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    First off, thanks for the help

    Here's a small program that illustrates my problem, at least when using vector::erase

    Code:
    #include <vector>
    
    using namespace std;
    
    typedef vector<int> INTVECTOR;
    INTVECTOR v_clients;
    
    int main(int argc, char **argv)
    {
    	INTVECTOR::iterator i;
    
    	v_clients.push_back(10);
    
    	for (i = v_clients.begin(); i != v_clients.end(); i++) {
    		v_clients.erase(i);
    	}
    
    	return 0;
    }
    My understanding is that the for loop should just interate through the elements, and remove them all, but it throws an exception. I realise that in this example I could just use pop_back, but this illustrates my problem in my actual program, in which an iterator is passed to the function which handles the connection.

    In my actual program, it's not a vector of integers, it's a vector of structs which holds information such as the SOCKET and sockaddr_in structure for the connection.

    Daniel

  4. #4
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Forgive me if it's a stupid error that I've made, I'm fairly new to C++ and I'm sure I can easily fall victim to common pitfalls and errors.

    Daniel

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You erase i, then you're trying to call the overloaded ++ operator on it. Not good. However, erase() returns the next iterator so use that instead:
    Code:
    i = v_clients.begin();
    while(i != v_clients.end()
    {
      i = v_clients.erase(i);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Alternatively, erase() comes overloaded with another version that takes a range of elements to be erased so you don't need to loop if you don't want to:

    v_clients.erase(v_clients.begin(), v_clients.end());

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, doing that he cannot call any functions on each element which he (implicitly) said he wanted. If the intention is to remove all elements, you may as well use v_clients.clear();
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks for the help guys, much appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about erase()
    By Sharke in forum C++ Programming
    Replies: 8
    Last Post: 06-10-2009, 01:22 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. string erase function problems
    By westm2000 in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2003, 10:19 PM