Click here to Skip to main content
13,861,301 members
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 24 Feb 2007

Checked Iterators

, 24 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Answers to some common questions on C++ checked iterators.


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:

vector<int>::iterator it;

Out of range access:

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

Comparing iterators from different ranges or containers:

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:

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:

vector<int> it = vec.begin();

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:

template <typename base_iterator, typename container_type>
class checked_iterator;     

and used like:

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.


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


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


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


About the Author

Nemanja Trifunovic
Software Developer (Senior) SAP
United States United States
Born in Kragujevac, Serbia. Now lives in Boston area with his wife and daughters.

Wrote his first program at the age of 13 on a Sinclair Spectrum, became a professional software developer after he graduated.

Very passionate about programming and software development in general.

You may also be interested in...

Comments and Discussions

QuestionFixed in VS2010 Pin
richy_b5-Sep-11 0:40
memberrichy_b5-Sep-11 0:40 
QuestionExamples? Pin
Shawn Poulson27-Feb-07 4:14
memberShawn Poulson27-Feb-07 4:14 
AnswerRe: Examples? Pin
Nemanja Trifunovic27-Feb-07 4:28
memberNemanja Trifunovic27-Feb-07 4:28 
GeneralRe: Examples? Pin
Shawn Poulson27-Feb-07 7:24
memberShawn Poulson27-Feb-07 7:24 
General&#1055;&#1086;&#1079;&#1076;&#1088;&#1072;&#1074; &#1080;&#1079; &#1050;&#1088;&#1072;&#1075;&#1091;&#1112;&#1077;&#1074;&#1094;&#1072; Pin
joxhy26-Feb-07 21:16
memberjoxhy26-Feb-07 21:16 
GeneralRe: Pin
Nemanja Trifunovic27-Feb-07 2:43
memberNemanja Trifunovic27-Feb-07 2:43 
QuestionBjarne Structure - who is he? Pin
gnk25-Feb-07 15:53
membergnk25-Feb-07 15:53 
AnswerRe: Bjarne Structure - who is he? Pin
Nemanja Trifunovic25-Feb-07 15:58
memberNemanja Trifunovic25-Feb-07 15:58 
AnswerRe: Bjarne Structure - who is he? Pin
Nemanja Trifunovic27-Feb-07 4:30
memberNemanja Trifunovic27-Feb-07 4:30 
GeneralSTLPort Pin
Trapper25-Feb-07 10:18
memberTrapper25-Feb-07 10:18 
GeneralRe: STLPort Pin
Nemanja Trifunovic25-Feb-07 10:42
memberNemanja Trifunovic25-Feb-07 10:42 
GeneralRe: STLPort Pin
Trapper25-Feb-07 10:49
memberTrapper25-Feb-07 10:49 
GeneralRe: STLPort Pin
Nemanja Trifunovic25-Feb-07 12:11
memberNemanja Trifunovic25-Feb-07 12:11 
GeneralRe: STLPort Pin
John M. Drescher25-Feb-07 14:27
memberJohn M. Drescher25-Feb-07 14:27 
GeneralVery Interesting! Pin
Roland Pibinger25-Feb-07 3:05
memberRoland Pibinger25-Feb-07 3:05 
GeneralRe: Very Interesting! Pin
Nemanja Trifunovic25-Feb-07 3:34
memberNemanja Trifunovic25-Feb-07 3:34 
GeneralSolid little write-up. Pin
Shog924-Feb-07 17:41
sitebuilderShog924-Feb-07 17:41 
GeneralRe: Solid little write-up. Pin
Nemanja Trifunovic25-Feb-07 3:23
memberNemanja Trifunovic25-Feb-07 3:23 
GeneralInformative Pin
PJ Arends24-Feb-07 16:34
professionalPJ Arends24-Feb-07 16:34 
GeneralRe: Informative Pin
Nemanja Trifunovic24-Feb-07 16:36
memberNemanja Trifunovic24-Feb-07 16:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 24 Feb 2007
Article Copyright 2007 by Nemanja Trifunovic
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid