欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

for循環(huán)中刪除map中的元素valgrind檢測(cè)提示error:Invalid read of size 8

 更新時(shí)間:2016年07月08日 13:55:04   作者:tanghuimin0713  
這篇文章主要介紹了for循環(huán)中刪除map中的元素valgrind檢測(cè)提示error:Invalid read of size 8 的相關(guān)資料,需要的朋友可以參考下

廢話不多說(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)文章

  • STL 的string類(lèi)怎么啦

    STL 的string類(lèi)怎么啦

    在我們研究string類(lèi)犯了什么毛病之前,還讓我先說(shuō)一下如何了解一個(gè)C++的類(lèi)。我們要了解一個(gè)C++的類(lèi),一般來(lái)說(shuō),要從三個(gè)方面入手
    2013-11-11
  • C++設(shè)計(jì)模式之單例模式

    C++設(shè)計(jì)模式之單例模式

    這篇文章主要介紹了C++設(shè)計(jì)模式之單例模式,本文同時(shí)給出了4種單例模式的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2014-09-09
  • 通俗易懂講解C語(yǔ)言與Java中二叉樹(shù)的三種非遞歸遍歷方式

    通俗易懂講解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-09
  • C++如何將字符串顛倒輸出

    C++如何將字符串顛倒輸出

    這篇文章主要介紹了C++如何將字符串顛倒輸出,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C語(yǔ)言復(fù)雜鏈表的復(fù)制實(shí)例詳解

    C語(yǔ)言復(fù)雜鏈表的復(fù)制實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言復(fù)雜鏈表的復(fù)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • C語(yǔ)言深入分析浮點(diǎn)型數(shù)據(jù)存儲(chǔ)

    C語(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-08
  • 在C/C++項(xiàng)目中合理使用宏詳解

    在C/C++項(xiàng)目中合理使用宏詳解

    這篇文章主要介紹了在C/C++項(xiàng)目中合理使用宏詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • C語(yǔ)言命令行參數(shù)的使用詳解

    C語(yǔ)言命令行參數(shù)的使用詳解

    本文主要介紹了C語(yǔ)言命令行參數(shù)的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(文件操作)

    C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(文件操作)

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),增加了文件操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C語(yǔ)言中正切的相關(guān)函數(shù)總結(jié)

    C語(yǔ)言中正切的相關(guān)函數(shù)總結(jié)

    這篇文章主要介紹了C語(yǔ)言中正切的相關(guān)函數(shù)總結(jié),包括正切和反正切以及雙曲線正切等的函數(shù),需要的朋友可以參考下
    2015-08-08

最新評(píng)論