为什么我可以更新 boost::fibonacci_heap 中的弹出元素?
Why can I update a popped out element in a boost::fibonacci_heap?
我正在使用 boost::fibonacci_heap 库实现 Fast Marching 算法,我开始使用它们的句柄来操纵元素。
我写了下面的基本代码,它编译得很好,但我不明白它的行为:
我首先将每个推送元素的句柄存储在一个数组中。然后我从堆中弹出第一个元素,我检查堆大小是否减少了。
但随后我尝试使用其句柄更新弹出的元素的值。这个操作有效(令人惊讶?),更新后的元素现在位于堆的顶部。但是堆大小保持不变,因为我没有正确地推入新元素。
那么当弹出的元素被更新并返回到堆中时会发生什么?堆仍然有效吗?
# include <boost/heap/fibonacci_heap.hpp>
# include <iostream>
using namespace std;
using namespace boost::heap;
struct node
{
int index;
double time;
node(const int& i, double t) : index(i), time(t) {}
};
struct compare_node
{
bool operator()(const node& n1, const node& n2) const
{
return (n1.time > n2.time);
}
};
int main()
{
fibonacci_heap< node, compare<compare_node> > heap;
typedef fibonacci_heap< node, compare<compare_node> >::handle_type handle_t;
handle_t tab_handle[10];
int n;
double tt[10];
// assign a set of arbitrary numbers to initialise array tt:
tt[0] = 5.3;
tt[1] = 0.25;
tt[2] = 3.6;
tt[3] = 1.75;
tt[4] = 9.1;
tt[5] = 2.54;
tt[6] = 3.8;
tt[7] = 6.1;
tt[8] = 1.23;
tt[9] = 7.32;
// push the values of tt into the heap, and keep track of the handle of each element in the heap:
for (n=0; n<10; n++)
{
tab_handle[n] = heap.push(node(n, tt[n]));
}
// display first element in heap
cout << "first element in heap is :" << heap.top().index << " " << heap.top().time << "\n";
// check size of the heap
cout << "size of heap is :" << heap.size() << "\n";
// remove top element
heap.pop();
// check size of the heap
cout << "size of heap after pop is :" << heap.size() << "\n";
// display first element in heap
cout << "first element in heap is now :" << heap.top().index << " " << heap.top().time << "\n";
// access value of a given element by index:
cout << "element index 2 has time :" << (*tab_handle[2]).time << "\n";
// attempt to access the element with handle tab_handle[1]: this element was actually popped out of the heap previously. But I can access it:
cout << "element index 1 has time :" << (*tab_handle[1]).time << "\n";
// update the time of an element using its handle. This is quite standard.
heap.update(tab_handle[2],node(2, tt[2]/10));
// but now I can also update the element that was popped out of the heap:
heap.update(tab_handle[1],node(1, tt[1]/10));
// display first element in heap
cout << "first element in heap is now :" << heap.top().index << " " << heap.top().time << "\n";
// check size of the heap
cout << "size of heap after pop is :" << heap.size() << "\n";
return 0;
}
终端输出:
first element in heap is :1 0.25
size of heap is :10
size of heap after pop is :9
first element in heap is now :8 1.23
element index 2 has time :3.6
element index 1 has time :0.25
first element in heap is now :1 0.025
size of heap after pop is :9
确实,您正在调用 Undefined Behaviour。
运行 您在 valgrind 下的程序¹:
sehe@desktop:/tmp$ valgrind --db-attach=yes ./test
==14185== Memcheck, a memory error detector
==14185== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==14185== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==14185== Command: ./test
==14185==
first element in heap is :1 0.25
size of heap is :10
size of heap after pop is :9
first element in heap is now :8 1.23
element index 2 has time :3.6
==14185== Invalid read of size 8
==14185== at 0x400F21: main (test.cpp:47)
==14185== Address 0x5aa6d78 is 24 bytes inside a block of size 72 free'd
==14185== at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14185== by 0x402789: __gnu_cxx::new_allocator<boost::heap::detail::marked_heap_node<node> >::deallocate(boost::heap::detail::marked_heap_node<node>*, unsigned long) (new_allocator.h:110)
==14185== by 0x401D2C: boost::heap::fibonacci_heap<node, boost::heap::compare<compare_node>, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_>::finish_erase_or_pop(boost::heap::detail::marked_heap_node<node>*) (fibonacci_heap.hpp:745)
==14185== by 0x4015E7: boost::heap::fibonacci_heap<node, boost::heap::compare<compare_node>, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_>::pop() (fibonacci_heap.hpp:398)
==14185== by 0x400E1C: main (test.cpp:34)
==14185==
==14185==
==14185== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ----
看,您正在读取之前删除的内存。哎呀
任何事情都有可能发生。会发生什么取决于你的运气,你的电脑等等
¹ 我使用了代码的清理版本:Live On Coliru,因此您可以交叉引用行号
我正在使用 boost::fibonacci_heap 库实现 Fast Marching 算法,我开始使用它们的句柄来操纵元素。
我写了下面的基本代码,它编译得很好,但我不明白它的行为:
我首先将每个推送元素的句柄存储在一个数组中。然后我从堆中弹出第一个元素,我检查堆大小是否减少了。
但随后我尝试使用其句柄更新弹出的元素的值。这个操作有效(令人惊讶?),更新后的元素现在位于堆的顶部。但是堆大小保持不变,因为我没有正确地推入新元素。
那么当弹出的元素被更新并返回到堆中时会发生什么?堆仍然有效吗?
# include <boost/heap/fibonacci_heap.hpp>
# include <iostream>
using namespace std;
using namespace boost::heap;
struct node
{
int index;
double time;
node(const int& i, double t) : index(i), time(t) {}
};
struct compare_node
{
bool operator()(const node& n1, const node& n2) const
{
return (n1.time > n2.time);
}
};
int main()
{
fibonacci_heap< node, compare<compare_node> > heap;
typedef fibonacci_heap< node, compare<compare_node> >::handle_type handle_t;
handle_t tab_handle[10];
int n;
double tt[10];
// assign a set of arbitrary numbers to initialise array tt:
tt[0] = 5.3;
tt[1] = 0.25;
tt[2] = 3.6;
tt[3] = 1.75;
tt[4] = 9.1;
tt[5] = 2.54;
tt[6] = 3.8;
tt[7] = 6.1;
tt[8] = 1.23;
tt[9] = 7.32;
// push the values of tt into the heap, and keep track of the handle of each element in the heap:
for (n=0; n<10; n++)
{
tab_handle[n] = heap.push(node(n, tt[n]));
}
// display first element in heap
cout << "first element in heap is :" << heap.top().index << " " << heap.top().time << "\n";
// check size of the heap
cout << "size of heap is :" << heap.size() << "\n";
// remove top element
heap.pop();
// check size of the heap
cout << "size of heap after pop is :" << heap.size() << "\n";
// display first element in heap
cout << "first element in heap is now :" << heap.top().index << " " << heap.top().time << "\n";
// access value of a given element by index:
cout << "element index 2 has time :" << (*tab_handle[2]).time << "\n";
// attempt to access the element with handle tab_handle[1]: this element was actually popped out of the heap previously. But I can access it:
cout << "element index 1 has time :" << (*tab_handle[1]).time << "\n";
// update the time of an element using its handle. This is quite standard.
heap.update(tab_handle[2],node(2, tt[2]/10));
// but now I can also update the element that was popped out of the heap:
heap.update(tab_handle[1],node(1, tt[1]/10));
// display first element in heap
cout << "first element in heap is now :" << heap.top().index << " " << heap.top().time << "\n";
// check size of the heap
cout << "size of heap after pop is :" << heap.size() << "\n";
return 0;
}
终端输出:
first element in heap is :1 0.25
size of heap is :10
size of heap after pop is :9
first element in heap is now :8 1.23
element index 2 has time :3.6
element index 1 has time :0.25
first element in heap is now :1 0.025
size of heap after pop is :9
确实,您正在调用 Undefined Behaviour。
运行 您在 valgrind 下的程序¹:
sehe@desktop:/tmp$ valgrind --db-attach=yes ./test
==14185== Memcheck, a memory error detector
==14185== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==14185== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==14185== Command: ./test
==14185==
first element in heap is :1 0.25
size of heap is :10
size of heap after pop is :9
first element in heap is now :8 1.23
element index 2 has time :3.6
==14185== Invalid read of size 8
==14185== at 0x400F21: main (test.cpp:47)
==14185== Address 0x5aa6d78 is 24 bytes inside a block of size 72 free'd
==14185== at 0x4C2C2BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14185== by 0x402789: __gnu_cxx::new_allocator<boost::heap::detail::marked_heap_node<node> >::deallocate(boost::heap::detail::marked_heap_node<node>*, unsigned long) (new_allocator.h:110)
==14185== by 0x401D2C: boost::heap::fibonacci_heap<node, boost::heap::compare<compare_node>, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_>::finish_erase_or_pop(boost::heap::detail::marked_heap_node<node>*) (fibonacci_heap.hpp:745)
==14185== by 0x4015E7: boost::heap::fibonacci_heap<node, boost::heap::compare<compare_node>, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_>::pop() (fibonacci_heap.hpp:398)
==14185== by 0x400E1C: main (test.cpp:34)
==14185==
==14185==
==14185== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ----
看,您正在读取之前删除的内存。哎呀
任何事情都有可能发生。会发生什么取决于你的运气,你的电脑等等
¹ 我使用了代码的清理版本:Live On Coliru,因此您可以交叉引用行号