最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

廢話不多說,先看下面一段代碼

#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;
}

上述代碼編譯運行皆沒有問題,但是用valgrind檢測會提示錯誤:

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?

此代碼可以實現(xiàn)功能要求,但是健壯性并不好,假設(shè)在map.erase之后再次使用map當前的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;
}
}

代碼運行就會出現(xiàn)錯誤,因為it目前指向的對象已經(jīng)被刪掉了。

為了避免程序出現(xiàn)這樣的錯誤,我們應(yīng)該保證在iterator指向的對象被刪掉之前,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檢測提示error:Invalid read of size 8 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • C++11原子操作詳解

    C++11原子操作詳解

    這篇文章主要為大家介紹了C++的原子操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • C語言指針的圖文詳解

    C語言指針的圖文詳解

    這篇文章主要為大家介紹了C語言指針,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • C++讀寫配置項的基本操作

    C++讀寫配置項的基本操作

    這篇文章主要介紹了C++讀寫配置項的基本操作,幫助大家更好的理解和學習c++,感興趣的朋友可以了解下
    2021-01-01
  • C++缺省參數(shù)的具體使用

    C++缺省參數(shù)的具體使用

    缺省參數(shù)是聲明或定義函數(shù)時為函數(shù)的參數(shù)指定一個默認值。本文就詳細的介紹了一下C++缺省參數(shù)的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2022-01-01
  • 深入分析C語言存儲類型與用戶空間內(nèi)部分布

    深入分析C語言存儲類型與用戶空間內(nèi)部分布

    這篇文章主要介紹了C語言存儲類型與用戶空間內(nèi)部分布,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-12-12
  • C++中關(guān)于constexpr函數(shù)使用及說明

    C++中關(guān)于constexpr函數(shù)使用及說明

    這篇文章主要介紹了C++中關(guān)于constexpr函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C語言左旋字符串的三種實現(xiàn)方式

    C語言左旋字符串的三種實現(xiàn)方式

    本文章將使用三種思路實現(xiàn)字符串的左旋(循環(huán)移動,截取和拼接,逆序反轉(zhuǎn)),文中通過代碼示例和圖文介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-03-03
  • 淺談C++11的std::function源碼解析

    淺談C++11的std::function源碼解析

    類模版std::function是一種通用的多態(tài)函數(shù)包裝器std::function的實例可以對任何可以調(diào)用的目標實體進行存儲、復(fù)制、和調(diào)用操作,本文詳細的介紹一下,感興趣的可以了解一下
    2021-06-06
  • Visual Studio 2019 Professional 激活方法詳解

    Visual Studio 2019 Professional 激活方法詳解

    這篇文章主要介紹了Visual Studio 2019 Professional 激活方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • Qt實現(xiàn)帶字數(shù)限制的文字輸入框

    Qt實現(xiàn)帶字數(shù)限制的文字輸入框

    這篇文章介紹了Qt實現(xiàn)帶字數(shù)限制文字輸入框的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04

最新評論

鄄城县| 观塘区| 承德县| 崇州市| 息烽县| 巫山县| 五大连池市| 烟台市| 永登县| 东明县| 萨迦县| 乌拉特前旗| 宁明县| 涞水县| 鄂温| 万年县| 乐山市| 方山县| 花垣县| 崇义县| 天津市| 淮南市| 克山县| 梁平县| 确山县| 基隆市| 抚州市| 安顺市| 贵州省| 厦门市| 玉门市| 兴仁县| 邓州市| 青河县| 盈江县| 曲靖市| 西安市| 自治县| 邵东县| 屯昌县| 玛沁县|