I always thought that retrieving the last element from an intrusive list just as easy as one two three. In fact, it is not. Let's dive into the story. Here I have the intrusive list declaration:
class FileNode : public boost::intrusive::list_base_hook<link_mode<auto_unlink> >
{
...
...
};
typedef boost::intrusive::list<FileNode, base_hook<list_base_hook<link_mode<auto_unlink> > >, constant_time_size<false> > FileNodeListType;
class FileBot
{
private:
FileNodeListType fileList;
...
...
};
And then I'll use the
iterator as shown below go straight to access the element located at the end of the list, but somehow this a hit segmentation fault error.
FileNodeListType::iterator it(fileList.end());
FileNode *p = &*it;
(*it).sibling.push_back(*node);
cout << p << endl;
The error is due to the wrong memory address is being accessed. As I verify on the element being inserted into the list (only one element is inserted in this test case) is having an address different from the one mention in the code above. Thus, according to the expert, in order to access the correct element located at the end of the list is by doing this:
FileNodeListType::iterator it(fileList.end());
FileNode *p = &*(--it);
cout << p << endl;
No comments:
Post a Comment