c++ - C++98/C++03 STL set bug? -
all,
one weird thing happens when i'm using set: let's say:
struct { int num; bool operator<(const a& x) const { return num < x.num; } }; struct b { set<a> aset; }; struct c { list<b> blist; list<b>::iterator blistiter; c() { blist.push_back(b()); blistiter = --(blist.end()); } };
somewhere can have code:
list<c> clist; clist.push_back(c()); list<c>::iterator clistiter = --(clist.end()); (...) { a = ...; ... clistiter->blistiter->aset.insert(a);
this end segmentation fault after few iterations. when happens, found invalid item referenced , compared (its address 0x0000001b, wrong) during insertion.
funny thing when turn on -std=c++0x option use c++11 problem goes aways! wonder if bug of stl set in c++98/c++03?
thanks,
kevin
in c++03 or earlier, copies of temporary objects put on clist
, blist
inside clist
object.
when c::blist
member copied, new list<b>
created, when c::blistiter
member copied, new c::blistiter
iterator still refers c::blist
list in original c
object - temporary gets destroyed. iterator refers element of destroyed list.
in c++11 move constructors used instead of copy constructors, resulting iterator doesn't refer dead object.
Comments
Post a Comment