启用 STL 迭代器调试到底有什么作用?
我通过定义在应用程序中启用了迭代器调试,
_HAS_ITERATOR_DEBUGGING = 1
我希望这实际上只是检查向量边界,但我有一种感觉它所做的远不止于此。实际上正在执行哪些检查等?
顺便说一句,Dinkumware STL。
I've enabled iterator debugging in an application by defining
_HAS_ITERATOR_DEBUGGING = 1
I was expecting this to really just check vector bounds, but I have a feeling it's doing a lot more than that. What checks, etc are actually being performed?
Dinkumware STL, by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有许多迭代器操作会导致未定义的行为,此触发器的目标是激活运行时检查以防止其发生(使用断言)。
问题
显而易见的操作是使用无效的迭代器,但这种无效性可能由多种原因引起:
vector
)[begin, end)
该标准在每个容器的令人难以忍受的细节中指定了哪个操作使哪个迭代器无效。
还有一个人们往往会忘记的不太明显的原因:将迭代器混合到不同的容器:
这涉及一个更普遍的问题:传递给算法的范围的有效性。
[cats.begin(), dogs.end())
无效(除非其中一个是另一个的别名)[cats.end(), cats.begin())
code> 无效(除非cats
为空??)解决方案
解决方案包括向迭代器添加信息,以便它们的有效性以及它们定义的范围的有效性可以在执行期间被断言,从而防止发生未定义的行为。
_HAS_ITERATOR_DEBUGGING
符号充当此功能的触发器,因为不幸的是它会减慢程序速度。理论上非常简单:每个迭代器都是发出它的容器的观察者,因此会收到修改通知。在 Dinkumware 中,这是通过两个附加功能来实现的:
这巧妙地解决了我们的问题:
成本
成本很重,但是正确性有代价吗?我们可以分解成本:
O(NbIterators)
O(NbIterators)
(请注意push_back
或insert
不一定会使所有迭代器无效,但erase
会)O( min(last-first,当然
,大多数库算法都是为了最大效率而实现的,通常在算法开始时一次性完成检查,然后运行未经检查的版本。然而速度可能会严重减慢,尤其是手写循环:
我们知道 Oops 行的品味很差,但这里更糟糕:每次运行循环时,我们都会创建一个新的迭代器销毁它,这意味着为 vec 的迭代器列表分配和取消分配节点...我是否必须强调在紧密循环中分配/取消分配内存的成本?
当然,
for_each
不会遇到这样的问题,这是使用 STL 算法而不是手工编码版本的另一个令人信服的案例。There is a number of operations with iterators which lead to undefined behavior, the goal of this trigger is to activate runtime checks to prevent it from occurring (using asserts).
The issue
The obvious operation is to use an invalid iterator, but this invalidity may arise from various reasons:
vector
)[begin, end)
The standard specifies in excruciating details for each container which operation invalidates which iterator.
There is also a somehow less obvious reason that people tend to forget: mixing iterators to different containers:
This pertain to a more general issue: the validity of ranges passed to the algorithms.
[cats.begin(), dogs.end())
is invalid (unless one is an alias for the other)[cats.end(), cats.begin())
is invalid (unlesscats
is empty ??)The solution
The solution consists in adding information to the iterators so that their validity and the validity of the ranges they defined can be asserted during execution thus preventing undefined behavior to occur.
The
_HAS_ITERATOR_DEBUGGING
symbol serves as a trigger to this capability, because it unfortunately slows down the program. It's quite simple in theory: each iterator is made anObserver
of the container it's issued from and is thus notified of the modification.In Dinkumware this is achieved by two additions:
And this neatly solves our problems:
The cost
The cost is heavy, but does correctness have a price? We can break down the cost:
O(NbIterators)
O(NbIterators)
(Note thatpush_back
orinsert
do not necessarily invalidate all iterators, buterase
does)O( min(last-first, container.end()-first) )
Most of the library algorithms have of course been implemented for maximum efficiency, typically the check is done once and for all at the beginning of the algorithm, then an unchecked version is run. Yet the speed might severely slow down, especially with hand-written loops:
We know the Oops line is bad taste, but here it's even worse: at each run of the loop, we create a new iterator then destroy it which means allocating and deallocating a node for
vec
's list of iterators... Do I have to underline the cost of allocating/deallocating memory in a tight loop ?Of course, a
for_each
would not encounter such an issue, which is yet another compelling case toward the use of STL algorithms instead of hand-coded versions.据我了解:
_HAS_ITERATOR_DEBUGGING 将在运行时显示一个对话框,以断言任何不正确的迭代器使用,包括:
1)擦除元素后在容器中使用的迭代器
2)在 .push() 或 .insert 之后在向量中使用的迭代器() 函数被调用
As far as I understand:
_HAS_ITERATOR_DEBUGGING will display a dialog box at run time to assert any incorrect iterator use including:
1) Iterators used in a container after an element is erased
2) Iterators used in vectors after a .push() or .insert() function is called
根据 http://msdn.microsoft。 com/en-us/library/aa985982%28v=VS.80%29.aspx
According to http://msdn.microsoft.com/en-us/library/aa985982%28v=VS.80%29.aspx