for循環(huán)中刪除map中的元素valgrind檢測(cè)提示error:Invalid read of size 8
廢話不多說(shuō),先看下面一段代碼
#include <iostream> #include <map> using namespace std; class A { public: typedef std::map<int, string> myMap; void mapInsert(int i, string s) { map.insert(std::make_pair(i, s)); } void deleteMap() { for (myMap::iterator it = map.begin(); it != map.end(); ++it) { map.erase(it->first); } } private: myMap map; }; int main() { A a; a.mapInsert(1, "1"); a.mapInsert(2, "2"); a.mapInsert(3, "3"); a.mapInsert(4, "4"); a.mapInsert(5, "5"); a.deleteMap(); return 0; }
上述代碼編譯運(yùn)行皆沒(méi)有問(wèn)題,但是用valgrind檢測(cè)會(huì)提示錯(cuò)誤:
valgrind --tool=memcheck --leak-check=full --track-origins=yes ./test # ~/test ==723953== Memcheck, a memory error detector ==723953== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==723953== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==723953== Command: ./test ==723953== ==723953== Invalid read of size 8 ==723953== at 0x3431C69E60: std::_Rb_tree_increment(std::_Rb_tree_node_base*) (tree.cc:60) ==723953== by 0x40131C: std::_Rb_tree_iterator<std::pair<int const, std::string> >::operator++() (in /home/thm/test/test) ==723953== by 0x40117C: A::deleteMap() (in /home/thm/test/test) ==723953== by 0x400F4B: main (in /home/thm/test/test) ==723953== Address 0x4c580b8 is 24 bytes inside a block of size 48 free'd ==723953== at 0x4A06016: operator delete(void*) (vg_replace_malloc.c:480) ==723953== by 0x401E23: __gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, std::string> > >::deallocate(std::_Rb_tree_node<std::pair<int const, std::string> >*, unsigned long) (in /home/thm/test/test) ==723953== by 0x401C99: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_put_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test) ==723953== by 0x401AA6: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_destroy_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test) ==723953== by 0x401729: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test) ==723953== by 0x40134C: std::map<int, std::string, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test) ==723953== by 0x401170: A::deleteMap() (in /home/thm/test/test) ==723953== by 0x400F4B: main (in /home/thm/test/test) ==723953== ==723953== Invalid read of size 8 ==723953== at 0x3431C69E80: std::_Rb_tree_increment(std::_Rb_tree_node_base*) (tree.cc:68) ==723953== by 0x40131C: std::_Rb_tree_iterator<std::pair<int const, std::string> >::operator++() (in /home/thm/test/test) ==723953== by 0x40117C: A::deleteMap() (in /home/thm/test/test) ==723953== by 0x400F4B: main (in /home/thm/test/test) ==723953== Address 0x4c580a8 is 8 bytes inside a block of size 48 free'd ==723953== at 0x4A06016: operator delete(void*) (vg_replace_malloc.c:480) ==723953== by 0x401E23: __gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, std::string> > >::deallocate(std::_Rb_tree_node<std::pair<int const, std::string> >*, unsigned long) (in /home/thm/test/test) ==723953== by 0x401C99: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_put_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test) ==723953== by 0x401AA6: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::_M_destroy_node(std::_Rb_tree_node<std::pair<int const, std::string> >*) (in /home/thm/test/test) ==723953== by 0x401729: std::_Rb_tree<int, std::pair<int const, std::string>, std::_Select1st<std::pair<int const, std::string> >, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test) ==723953== by 0x40134C: std::map<int, std::string, std::less<int>, std::allocator<std::pair<int const, std::string> > >::erase(std::_Rb_tree_iterator<std::pair<int const, std::string> >) (in /home/thm/test/test) ==723953== by 0x401170: A::deleteMap() (in /home/thm/test/test) ==723953== by 0x400F4B: main (in /home/thm/test/test) ==723953== ==723953== ==723953== HEAP SUMMARY: ==723953== in use at exit: 0 bytes in 0 blocks ==723953== total heap usage: 10 allocs, 10 frees, 370 bytes allocated ==723953== ==723953== All heap blocks were freed -- no leaks are possible ==723953== ==723953== For counts of detected and suppressed errors, rerun with: -v ==723953== ERROR SUMMARY: 8 errors from 2 contexts (suppressed: 6 from 6)
Why?
此代碼可以實(shí)現(xiàn)功能要求,但是健壯性并不好,假設(shè)在map.erase之后再次使用map當(dāng)前的iterator,即
void deleteMap() { for (myMap::iterator it = map.begin(); it != map.end(); ++it) { map.erase(it->first); std::cout << "map.first=" << it->first << " map.second=" << it->second << std::endl; } }
代碼運(yùn)行就會(huì)出現(xiàn)錯(cuò)誤,因?yàn)閕t目前指向的對(duì)象已經(jīng)被刪掉了。
為了避免程序出現(xiàn)這樣的錯(cuò)誤,我們應(yīng)該保證在iterator指向的對(duì)象被刪掉之前,iterator已經(jīng)向前移位一。
程序改成如下即可:
void deleteMap() { for (myMap::iterator it = map.begin(); it != map.end();) { map.erase(it++->first); } }
或
void deleteMap() { for (myMap::iterator it = map.begin(); it != map.end();) { int i = it->first; ++it; map.erase(i); } }
以上所述是小編給大家介紹的for循環(huán)中刪除map中的元素valgrind檢測(cè)提示error:Invalid read of size 8 ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
通俗易懂講解C語(yǔ)言與Java中二叉樹(shù)的三種非遞歸遍歷方式
二叉樹(shù)是一種非常重要的數(shù)據(jù)結(jié)構(gòu),很多的數(shù)據(jù)結(jié)構(gòu)都是基于二叉樹(shù)的基礎(chǔ)演變過(guò)來(lái)的。二叉樹(shù)的前,中,后3種遍歷方式,因?yàn)闃?shù)的定義本身就是遞歸定義的,所以采用遞歸的方法來(lái)實(shí)現(xiàn)是很簡(jiǎn)單的2021-09-09C語(yǔ)言復(fù)雜鏈表的復(fù)制實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言復(fù)雜鏈表的復(fù)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02C語(yǔ)言深入分析浮點(diǎn)型數(shù)據(jù)存儲(chǔ)
使用編程語(yǔ)言進(jìn)行編程時(shí),需要用到各種變量來(lái)存儲(chǔ)各種信息。變量保留的是它所存儲(chǔ)的值的內(nèi)存位置。這意味著,當(dāng)您創(chuàng)建一個(gè)變量時(shí),就會(huì)在內(nèi)存中保留一些空間。您可能需要存儲(chǔ)各種數(shù)據(jù)類(lèi)型的信息,操作系統(tǒng)會(huì)根據(jù)變量的數(shù)據(jù)類(lèi)型,來(lái)分配內(nèi)存和決定在保留內(nèi)存中存儲(chǔ)什么2022-08-08C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(文件操作)
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),增加了文件操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C語(yǔ)言中正切的相關(guān)函數(shù)總結(jié)
這篇文章主要介紹了C語(yǔ)言中正切的相關(guān)函數(shù)總結(jié),包括正切和反正切以及雙曲線正切等的函數(shù),需要的朋友可以參考下2015-08-08