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:
/***** FileNode.h *****/
class FileNode : public boost::intrusive::list_base_hook<link_mode<auto_unlink> >
{
...
...
};
/***** FileBot.h *****/
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());
// following piece basically doing some data manipulation
// on the element retrieve from the list.
FileNode *p = &*it;
(*it).sibling.push_back(*node);
cout << p << endl; // memory address shows 0x7ffedff8e808
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; // memory address shows 0xa01bc0
No comments:
Post a Comment