Checked Iterators

 http://www.codeproject.com/KB/stl/checkediterators.aspx

 

vc8默认在debug下和release下都打开这个选项,导致stl的效率大幅降低,通常情况下,应该在debug的时候使用checked模式,在release下关闭掉他。

Introduction

Checked iterators are something I have known about for a while but never really bothered to use or understand better, until Microsoft Visual C++ 2005 was released. It came with a checked version of the Standard Library even in release mode and when porting the code from other compilers, I saw in practice both good and bad sides of using checked iterators. After working with checked iterators for a year and doing some research on the topic, I feel it is a good time to share the knowledge I gathered with the Code Project community. The article is structured as a series of questions and answers in an attempt to be short and an easy read.

Questions and Answers

What are checked iterators?

Checked iterators offer checked access to containers and other ranges. They are typically aware of the owning container and are able to catch some run-time errors that often lead to undefined behavior and then do something that is well-defined, like throwing an exception or terminating the application. This is particularly useful in avoiding security-related problems such as stack and heap overflows.

What kind of errors do checked iterators detect?

That really depends on their implementation. Some of the common errors they can catch are:

Uninitialized iterators:

 Collapse code snippet
vector<int>::iterator it;
it++;     

Out of range access:

 Collapse
vector<int>::iterator it = vec.end();
it++;      

Comparing iterators from different ranges or containers:

 Collapse
for (it = vec1.begin(); it != vec2.end(); ++it)     
Can checked iterators detect other types of errors, such as invalid data in containers/ranges?

Yes. For instance, the UTF-8 CPP library version 2.0 or later has checked iterator adapters that are able to detect invalid utf-8 sequences:

 Collapse
utf8::iterator<std::string::iterator> it(u8string.begin(), 
                                         u8string.begin(), u8string.end());
it++; // throws an exception in case of an invalid utf-8 sequence

Note that we passed the valid range boundaries as parameters to the constructor of the iterator adapter.

Are there checked containers as well, and if yes what are they good for?

Just as checked iterators keep track of their containers, containers can keep track of the iterators. That enables catching errors such as use of invalidated or "dangling" iterators:

 Collapse
vector<int> it = vec.begin();
vec.clear();
it++;     
Is there an easy way to turn an unchecked iterator into a checked one?

Depends. Some STL implementations, like Dinkumware and STLPort offer both checked and unchecked versions, and switching between them is a simple matter of setting specific macros to a desired value. This is particularly handy if you want to use checked iterators in the debug mode and unchecked ones in the release mode, which is a pretty common scenario.

Other than that, you can write a checked iterator adapter that would turn an unchecked iterator into a checked one. In his classic book The C++ Programming Language, Bjarne Stroustrup gives an example of such an iterator adapter. In general, it could be declared something like:

 Collapse
template <typename base_iterator, typename container_type>
class checked_iterator;     

and used like:

 Collapse
checked_iterator<myvectype::iterator, myvectype> it (vecit, vec);
it++; // checked

Of course, such adapters are a poor man's replacement for a full-fledged checked STL implementation, but still may be very helpful.

Are there downsides to using checked iterators?

Yes - performance. Checked iterators are fatter and slower than their unchecked counterparts, and the difference in speed (rarely in memory footprint) can be significant. Sometimes it is a good idea to use checked iterators only during development and the first round of testing, and ship the software with unchecked iterators, but that really depends on a concrete situation.

Does Microsoft Visual C++ come with checked iterators?

Yes. In version 8.0 (VC++ 2005) the iterators and containers are checked by default even in release builds. To turn the checked iterators off, the _SECURE_SCL macro must be set to 0. By default, in case an error is detected, the program gets terminated by calling invalid_parameter. This behavior may be changed by setting the _SECURE_SCL_THROWS to 1, and in this case checked iterators throw standard exceptions in case of error.

Conclusion

Checked iterators are a tool you should know about and use appropriately, at least during development.

References

  1. Bjarne Stroustrup: The C++ Programming Language.
  2. Herb Sutter, Andrei Alexandrescu: C++ Coding Standards.
  3. MSDN Library: Checked Iterators

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值