i've come across stupid bug in code wrote. after spending time looking through debugger, discovered find quite strange. consider following incorrect, trivial code.
#include <map> #include <list> int main() { std::map<int, std::list<int> > mymap; // infinite loop, should std::pair<int, std::list<int> > mymap.insert(std::pair<int, int>(4, 500000)); return 0; } as comment indicates, insert statement causes program enter infinite loop. cause of quite obvious, i've passed in std::pair<int, int> object instead of std::pair<int, std::list<int>>. unfortunately, code compiles fine in both gcc , msvc10. expect compiler reject code because types don't match, didn't. care explain why?
edit: appears work fine in gcc (the site used didn't work correctly), msvc10 still accepts it.
edit again: believe crash caused fact that, in original code, insertion inserting like:
mymap.insert(std::pair<int, int>(4, id)) id potentially large. never raised memory exceptions, guess spending lot of time allocating (without failing), why appeared loop. so, seems msvc happy implicit conversion std::list, gcc isn't. confusing, according http://www.cplusplus.com/reference/list/list/list/ constructors std::list marked explicit. looks bug msvc10, msvc11 rejects code (as should).
i tried compiling in visual studio 2010
microsoft visual studio 2010 version 10.0.40219.1 sp1rel it compiled, did not enter infinite loop. instead did following...
std::map<int, std::list<int> > mymap; mymap.insert(std::pair<int, int>(4, 5)); // implicit cast??? std::cout << "mymap contains " << mymap.size() << " elements.\n"; std::cout << "mymap[4] size " << mymap[4].size() << '\n'; sel = mymap[4].begin(); end = mymap[4].end(); for(; sel != end; ++sel) std::cout << *sel << ", "; std::cout << std::endl; outputs following
mymap contains 1 elements. mymap[4] size 5 0, 0, 0, 0, 0, so, think reason working list being created using following default constructor:
explicit list (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type()); so creating you, 4 mapping list of 5 elements, each filled value_type().
my guess somehow msvc compiler casting std::pair<int, int> std::pair<int, std::list<int>> using above constructor list...
that goes way answering why compiles without complaining... doesn't explain infinite loop... don't know :)
Comments
Post a Comment