Trashes in copied std::list

前提是你 提交于 2019-12-06 05:32:47

gdb is probably getting confused by an implementation detail the std::list. E.g. the old SGI STL list was implemented as a circular list. Inside the list object, there is only a singly _List_node<_Tp> pointer called _M_node. The constructor puts the internal _M_next pointer of the final node element equal to _M_node itself.

The reason Standard Library implementations of std::list use this circular implementation is to avoid special cases for the final element (e.g. they could also use a a sentinel element with a nullptr next pointer). Matt Austern has a nice ACCU presentation about this (but the link is currently to a corrupted file, see archived version here).

This circular implementation explains why your gdb output for g.neighbors() has the repeating pattern of [0] = 2, [1] = 1, [2] = 4294956560, /* etcetera */. The value 4294956560 is simply the memory address of the internal _M_node variable of your std::list, so if gdb only does simnple pointer chasing, it will get confused. Notice that it is less than 2^32, i.e. you are probably compiling this for 32-bits.

You should probably verify this in your own <list> header of the Standard Library on your system. A bug report for gdb might also be in order.

I think that the problem is on the copy constructor, just do this:

AdjList(const AdjList& g) : AdjList(g.m_nodes_count) {
    m_list = g.m_list ; 
}

The operator=() method should create a new Vector<Row> with the same nodes that g.m_list has.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!