CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2012
    Posts
    2

    map/set iterators incompatible - what might be causing it?

    I recently started programming in c++ with visual studio 2010, my current projekt (or solution) compiles without errors. Then I start the program and then does the program crach with the following error.
    Code:
    Debug Assertion Failed!
    ...
    Expression: map/set iterators incompatible
    I have tried to understand why, and what is causing it but no success. Do anyone know what might be causing it? Program crach when this code run
    PHP Code:
    for(set<Square*>::iterator itvitv != vv.end(); itv++ )
    // think the problem is at 'itv != vv.end()' 
    PHP Code:
        set<Square*> set1;
        
    map<Square*, set<Square*>> map_set;
        for(
    int i=0;i<9;i++){
            for(
    int j=0;j<9;j++){
                
    Squaresq sqs[i][j];
                
    set<Square*> un_v sq->getNeighbours();
                
    map_set.insert(pair<Square*, set<Square*>>(squn_v));
            }
        }
        
    typedef map<Square*, set<Square*>>::iterator it_map;
        for(
    set<Square*>::iterator it_m set1.begin(); it_m != set1.end(); it_m++){
            
    Squaresq1 = (*it_m); // square 1
            
    for(it_map it map_set.begin();it != map_set.end(); it++){
                
    set<Square*> vv it->second;
                
    set<Square*> v;// = set<Square*>(vv.begin(), vv.end());
                
    for(set<Square*>::iterator itvitv != vv.end(); itv++ ){ // error here <------
                    
    v.insert(new Square(**itv));
                }
                
    cout << "DEBUGG MSG" << endl// never printed
            
    }
        } 

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: map/set iterators incompatible - what might be causing it?

    Quote Originally Posted by cltx View Post
    I recently started programming in c++ with visual studio 2010, my current projekt (or solution) compiles without errors.
    Compiling without errors has no bearing on whether the program will run successfully.
    Then I start the program and then does the program crach with the following error.
    And have you debugged your code? You are using Visual C++ which has one of the best debuggers ever developed. When you debug your program using the debugger where does it cause the error?

    Secondly, having pointer values as keys is highly dangerous and isn't recommended -- all it will do is eventually lead to bugs that are very hard to find.

    Look at your map -- you are using a pointer value (Square*) as a key. So what happens if that pointer is deallocated? You now have to scramble and make sure the map is updated also to remove this, or else you now have an invalid entry in the map. Worse yet, what if that pointer is deleted, and then another call to "new" returns the same pointer value? You now have a map entry that may be referencing old data in the map.

    Look at your set<Square*> -- you are using a pointer value as the determining factor, and pointer values can change, become invalid, etc. Again you will be having one heck of a time making sure your set is updated whenever that pointer changes, gets invalidated, etc. If your program gets bigger, at some point you will be fighting a losing battle trying to keep things straight.

    The key to a map or objects in a std::set should be something that can't give these issues, types such as int, long, std::string, etc. or a class that has a well-defined operator <. Pointers should not be used as keys to a map or as foundations in any other data structure such as a std::set (even though it is allowable).

    Also, it would be much easier for you to typedef your declarations:
    Code:
    #include <set>
    #include <map>
    //...
    typedef std::set<Square*> SquareSet;
    typedef std::map<Square*, SquareSet> MapOfSquareSet;
    //...
    Then you use the typedef name instead of typing each and every time "std::set<blah>".

    As to your error:
    Code:
     for( set<Square*>::iterator itv; itv != vv.end(); itv++ ){
    itv is not initialized. Look closely at the part in red.

    Also, you should use preincrement in your loop, not post-increment:
    Code:
    for(set<Square*>::iterator it_m = set1.begin(); it_m != set1.end(); ++it_m){
    Last, I can bet you're overusing pointers. Why do you need to have a Square* to begin with? Why not just deal with Square objects?

    If there is no reason to use pointers, then don't use them. The more value-based and the less pointer-based C++ program can be, the less bugs at runtime you will have. Only when it is demonstrably necessary to use pointers and dynamically allocated memory should you do so.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 29th, 2012 at 05:21 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured