Aaron N. Tubbs bio photo

Aaron N. Tubbs

Dragon chaser.

Twitter Facebook Google+ LinkedIn Github

Buried a little bit down in the Visual Studio 2005 release notes, Microsoft provides this little tidbit about changes to the C++ standard library support:

The Standard C++ Library now has support to detect incorrect iterator use. See Debug Iterator Support and Checked Iterators for more information.

The Debug Iterator Support bit is cool. Do something stupid at runtime (like using an invalidated iterator) in debug mode and it will toss an assertion and crash.


Checked Iterators are much more upsetting, however. Consider this code snippet (messy and cluttered to illustrate, this isn’t real code), meant, for the sake of argument, to provide an index to some legacy code:

std::vector<int> list;
for (int i = 0; i < 100; i++) list.push_back(i);
std::vector<int>::iterator a = list.begin;
std::vector<int>::iterator b(a);
someLegacyCode(5 + b - a);

Now, before Visual Studio 2005, this would pass 4 to someLegacyCode. In truth, this is just obfuscated pointer arithmetic, but the iterators have evolved to be far more much than simple pointer wrappers over time.

In Visual Studio 2005, the behavior of this code changes by default. ‘5 + b’ will cause the program will crash, because inside of operator+ code is triggered that detects an iterator passing outside of container boundaries.

A lot of the changes made to C++ and the standard library in Visual Studio 2005 was to make the compiler more compliant, and that’s cool. However, in implementing checked iterators by default, they enable a behavior that is not standard, as best as I can research: At runtime, this program will assert and then crash in debug mode. In release mode, this code will crash.

Let me repeat that. In their zeal to make C++ and the STL safer, Microsoft added code that by default in release mode increases overhead and will crash the program.

Please, leave these types of decisions to the C++ standards guys, Microsoft, and go back to what you’re good at — implementing somebody else’s bright ideas.

I’m not trying to defend the above code, but I’d really like to meet the compiler guys at Microsoft that think they can justify that making this change to be a new default, and not something optional or debug-only.

Update: Fixed my example/explanation, as it was wrong.