gpt4 book ai didi

c++ - STL vector 的大规模删除导致我的二进制文件崩溃

转载 作者:行者123 更新时间:2023-11-28 00:44:58 26 4
gpt4 key购买 nike

我的二进制文件崩溃了。在运行核心转储时,我发现了以下内容:

#0  0x00a6a363 in memmove () from /lib/tls/libc.so.6
(gdb) frame 1
#1 0x083a108c in std::__copy_trivial<piola::piolaOrderBook*> (__first=0xb277f2c4, __last=0xb277f2bc, __result=0xb277f2c0)
at /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_algobase.h:258
258 std::memmove(__result, __first, sizeof(_Tp) * (__last - __first));
(gdb) frame 2
#2 0x083a0ad6 in std::__copy_aux2<piola::piolaOrderBook*> (__first=0xb277f2c4, __last=0xb277f2bc, __result=0xb277f2c0)
at /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_algobase.h:279
279 { return std::__copy_trivial(__first, __last, __result); }
(gdb) frame 3
#3 0x083a02d1 in std::__copy_ni2<piola::piolaOrderBook**, __gnu_cxx::__normal_iterator<piola::piolaOrderBook**, std::vector<piola::piolaOrderBook*, std::allocator<emapi::EmapiOrderBook*> > > > (__first=0xb277f2c4, __last=0xb277f2bc, __result=
{_M_current = 0xb277f2c0})
at /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_algobase.h:296
296 return _OutputIterator(std::__copy_aux2(__first, __last, __result.base(),
(gdb) frame 4
#4 0x0839f1b0 in std::__copy_ni1<__gnu_cxx::__normal_iterator<piola::piolaOrderBook**, std::vector<piola::piolaOrderBook*, std::allocator<piola::piolaOrderBook*> > >, __gnu_cxx::__normal_iterator<piola::piolaOrderBook**, std::vector<piola::piolaOrderBook*, std::allocator<piola::piolaOrderBook*> > > > (__first={_M_current = 0xb277f2c4}, __last=
{_M_current = 0xb277f2bc}, __result={_M_current = 0xb277f2c0})
at /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_algobase.h:317
317 return std::__copy_ni2(__first.base(), __last.base(),
(gdb) frame 5
#5 0x0839d676 in std::copy<__gnu_cxx::__normal_iterator<piola::piolaOrderBook**, std::vector<piola::piolaOrderBook*, std::allocator<piola::piolaOrderBook*> > >, __gnu_cxx::__normal_iterator<piola::piolaOrderBook**, std::vector<piola::piolaOrderBook*, std::allocator<piola::piolaOrderBook*> > > > (__first={_M_current = 0xb277f2c4}, __last={_M_current = 0xb277f2bc},
__result={_M_current = 0xb277f2c0})
at /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/stl_algobase.h:358
358 return std::__copy_ni1(__first, __last, __result, __Normal());
(gdb)

其中大部分内容对我来说都很神秘,但是在查找 memmove 时,代码似乎崩溃了,因为它无法处理 vector 中的删除(因为从 vector 中删除对于大型 vector 来说是一项非常繁重的操作)?

我说得对吗?如果是,我该如何解决这个问题(当然除了修复设计之外)?

代码在这里:

for (orderbkIterator = vOrderBook.begin(); orderbkIterator != vOrderBook.end(); orderbkIterator++)
{

if ( (*(*orderbkIterator)->getOrderBookId()) == *(TradableInst->getOrderBookId()) )
{
long long a = (*(*orderbkIterator)->getOrderBookId());
ADDVLOG(LOG_INFO, "Removing record (%lld) from vOrderBook", a );
vOrderBook.erase(orderbkIterator);
}

最佳答案

来自 std::vector::erase() :

Iterators and references to the erased elements and to the elements between them and the end of the container are invalidated. Past-the-end iterator is also invalidated.

所以 orderbkIterator 将在下一次递增时无效,如果 erase() 被调用。更改循环的结构,因为 erase() 返回被删除的迭代器之后的下一个迭代器,这意味着只有在 erase() 没有发生时才会递增:

for (orderbkIterator = vOrderBook.begin(); orderbkIterator != vOrderBook.end();)
{
if (...)
{
orderbkIterator = vOrderBook.erase(orderbkIterator);
}
else
{
++orderbkIterator;
}
}

关于c++ - STL vector 的大规模删除导致我的二进制文件崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16696334/

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