STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树

STL map 内存改变,迭代器失效,_Isnil(_Ptr)和红黑树

最近在做项目时发现一个crash的问题,当时得到的dmp文件显示crash在一个以map为循环变量的循环中,crash位置在如下的代码中标出。

 

        void _Inc()
            {    // move to node with next larger value

 #if _HAS_ITERATOR_DEBUGGING
            if (this->_Mycont == 0
                || _Ptr == 0
                || _Isnil(_Ptr))
                {
                _DEBUG_ERROR("map/set iterator not incrementable");
                _SCL_SECURE_OUT_OF_RANGE;
                }
 #else
            _SCL_SECURE_VALIDATE(this->_Has_container());
            if (_Isnil(_Ptr))---------------------------------------->Why crash here?
                {
                _SCL_SECURE_OUT_OF_RANGE;
                // end() shouldn't be incremented, don't move if _SCL_SECURE is not turned on
                }
 #endif /* _HAS_ITERATOR_DEBUGGING */

            else if (!_Isnil(_Right(_Ptr)))
                _Ptr = _Min(_Right(_Ptr));    // ==> smallest of right subtree
            else
                {    // climb looking for right subtree
                _Nodeptr _Pnode;
                while (!_Isnil(_Pnode = _Parent(_Ptr))
                    && _Ptr == _Right(_Pnode))
                    _Ptr = _Pnode;    // ==> parent while right subtree
                _Ptr = _Pnode;    // ==> parent (head if end())
                }
            }

 

这是C++ 中红黑树迭代器的标准实现,那从这个栈帧能说明我们的代码哪出问题了么?在阅读红黑树的实现代码中有一条语句困扰了我大约半个小时的时间,这条语句就是:

_Isnil(_Ptr)

标准实现中到处都是这条语句,2年前算法导论系统的学习过一遍,但是由于长时间没有相关的功能需要用到这么深入的知识,有些具体的问题已经记得不是很清楚,于是为了弄对这条语句以及对红黑树有透测的理解,再一次对红黑树知识进行了系统的学习,并翻阅了一些资料,为了使本文自成体系,下面对基础知识进行一些说明。

红黑树定义

红黑树是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,典型的用途是实现关联数组,如STL map set,和普通二叉树相比它的实现上稍微有些复杂,但它的操作有着良好的最坏情况运行时间,并且在实践中是高效的: 它可以在O(log n)时间内做查找,插入和删除,这里的n是树中元素的数目。

红黑树性质

红黑树是每个节点都带有颜色属性的二叉查找树,颜色为红色或黑色。在二叉查找树强制一般要求以外,对于任何有效的红黑树我们增加了如下的额外要求:

性质1. 节点是红色或黑色。

性质2. 根是黑色。

性质3. 所有叶子都是黑色(叶子是NIL节点)。

性质4. 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)

性质5. 从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点。

算法导论原版定义

A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can be either RED or BLACK. By constraining the node colors on any simple path from the root to a leaf, red-black trees ensure that no such path is more than twice as long as any other, so that the tree is approximately balanced. Each node of the tree now contains the attributes color, key, left, right, and p. If a child or the parent of a node does not exist, the corresponding pointer attribute of the node contains the value NIL. We shall regard these NILs as being pointers to leaves (external nodes) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree. A red-black tree is a binary tree that satisfies the following red-black properties:

1. Every node is either red or black.

2. The root is black.

3. Every leaf (NIL) is black.

4. If a node is red, then both its children are black.

5. For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

典型的红黑树如下图所示

 

红黑树的典型操作

插入/删除/左旋/右旋/查找/遍历再次不做赘述,有兴趣的读者可以参考STL源码实现。

总结

再来看看我们的问题,请看如下的迭代器实现,迭代器++操作符会调用_Inc(),由于多线程,map被破坏,迭代器失效导致循环无法结束并crash。那么为什么会crash呢?原来红黑树的标准实现中真正的叶子节点都是NIL(哨兵)节点,并且规定叶子节点已经到达了红黑树的边界,所以不能在++,如果在++系统就会 crash

const_iterator& operator++()

{      // preincrement

       _Inc();

return (*this);

}

那么这条语句:_Isnil(_Ptr) 到底是什么意思呢?

答案:就是说当前操作的迭代器是否已经指向了黑色的叶子节点,这个节点其实是哨兵(NIL)节点,指向了这个哨兵节点以后的迭代器是不能++的。

原文地址:https://www.cnblogs.com/pugang/p/2558542.html