gpt4 book ai didi

c++ - STL Set inside Map ,奇怪的段错误和Valgrind分析

转载 作者:行者123 更新时间:2023-11-28 07:46:22 25 4
gpt4 key购买 nike

我的代码中有一个奇怪的段错误,它使用以下数据结构:

map< uint64_t, set<uint64_t> > _key_to_block;

Valgrind 使用此消息提示 _key_to_block.erase(it):

Address 0x6106118 is 56 bytes inside a block of size 88 free'd

像这样从 map 中删除一个元素:

map< uint64_t, set<uint64_t> >::iterator it     = _key_to_block.find(key);
(it->second).clear();
_key_to_block.erase(it);

此外,Valgrind 还提示 (it->second).insert(k); 有这样的按摩:

Invalid read of size 8

像这样在 STL 集中插入一个元素:

map< uint64_t, set<uint64_t> >::iterator it = _key_to_block.find(key);
(it->second).insert(value);

但是,它不会提示这一行:

setit = it->second.find(value);

有什么想法吗?

最佳答案

我没有看到任何针对 end() 的检查,所以我猜测您的 find 调用实际上没有找到 key 并返回end 迭代器。一旦你开始取消引用它,所有关于行为的赌注都会被取消。您应该使用 operator[] 始终创建元素,或者根据 end 检查您的 find 的结果。

关于c++ - STL Set inside Map ,奇怪的段错误和Valgrind分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14804030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com