From: itaj sherman on 3 Mar 2010 03:33 Is there a good reason the following code causes access violation in MSVC6? Using std::map::erase, like: aMap.erase( aMap.rbegin().base() ); #include <string> #include <map> typedef std::string cKey; typedef std::string cElement; typedef std::map< cKey, cElement > cMap; int main(int argc, char* argv[]) { cMap aMap; aMap[ cKey("k1") ] = cElement("v1"); while( !aMap.empty() ) { aMap.erase( aMap.rbegin().base() ); //this line causes access violation in MSVC6 } return 0; } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jared Wein on 3 Mar 2010 17:21 On Mar 3, 3:33 pm, itaj sherman <itajsher...(a)gmail.com> wrote: > Is there a good reason the following code causes access violation in > MSVC6? > Using std::map::erase, like: aMap.erase( aMap.rbegin().base() ); > > #include <string> > #include <map> > > typedef std::string cKey; > typedef std::string cElement; > typedef std::map< cKey, cElement > cMap; > > int main(int argc, char* argv[]) > { > > cMap aMap; > > aMap[ cKey("k1") ] = cElement("v1"); > > while( !aMap.empty() ) { > aMap.erase( aMap.rbegin().base() ); //this line causes access > violation in MSVC6 > } > > return 0; > > } > > -- > [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ] > [ comp.lang.c++.moderated. First time posters: Do this! ] Reproduced in VC10. I believe this would happen because std::map::rbegin() will be pointing to the position *after* the last element. If you switch the code to use std::map::rend() then your problem will go away. Another way to look at the problem is that if you try using std::map::end() the same AV will happen. Swapping in std::map::begin() will work though. HTH - jared -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: itaj sherman on 4 Mar 2010 06:18 On Mar 4, 12:21 pm, Jared Wein <weinja...(a)gmail.com> wrote: > On Mar 3, 3:33 pm, itaj sherman <itajsher...(a)gmail.com> wrote: > > > Is there a good reason the following code causes access violation in > > MSVC6? > > Using std::map::erase, like: aMap.erase( aMap.rbegin().base() ); > > > #include <string> > > #include <map> > > > typedef std::string cKey; > > typedef std::string cElement; > > typedef std::map< cKey, cElement > cMap; > > > int main(int argc, char* argv[]) > > { > > > cMap aMap; > > > aMap[ cKey("k1") ] = cElement("v1"); > > > while( !aMap.empty() ) { > > aMap.erase( aMap.rbegin().base() ); //this line causes access > > violation in MSVC6 > > } > > > return 0; > > > } > > Reproduced in VC10. > > I believe this would happen because std::map::rbegin() will be > pointing to the position *after* the last element. If you switch the > code to use std::map::rend() then your problem will go away. > > Another way to look at the problem is that if you try using > std::map::end() the same AV will happen. Swapping in std::map::begin() > will work though. { edits: quoted block of blank lines, signature and two quoted banners removed. please keep readers in mind when quoting. -mod } Right, thanks, I should have done the following: while( !aMap.empty() ) { aMap.erase( (++aMap.rbegin()).base() ); } I need to remove the last element in the map, not the first. I had a few problems with VC6 lately, so I tend to blame it for everything now. Sadly management can't schedule upgrade now. itaj -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Pages: 1 Prev: C++0x variadic tuple data members Next: Conversion to non-scalar type |