Jump to content
  • Advertisement
Sign in to follow this  
Storyyeller

stl debug instrumentation

This topic is 2480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any system that can automatically instrument STL iterators to verify correctness, while being compiled out for release builds? Recently I spent hours debugging a problem which turned out to be due to the use of set.erase(set.find(x)), which is undefined behavior if x is not in the set. It would be nice if there was an easier way to detect errors like that while testing.

Share this post


Link to post
Share on other sites
Advertisement
Depends. Visual C++ has some iterator debugging it uses in release builds (which you have to explicitly disable if you don't want it). I'm not sure exactly how it's all implemented and whether or not it could've helped in this situation.

Perhaps a more pertinent question though, what platform and compiler is this for? The C++ standard doesn't dictate any such error checking, so it'll probably depend on the compiler/version of the Standard Library that you're using.

[edit]

And of course, ninja'd by fastcall22's insights.

Share this post


Link to post
Share on other sites

And of course, ninja'd by fastcall22's insights.




[quote name='fastcall22' timestamp='1320197666' post='4879485']
set.erase(x) ...

I figured that out afterwards. I'm just wondering about ways to prevent similar errors in the future.
[/quote]

I actually deleted my post a few seconds later, because I realized that set.erase(set.find(x)) has differing behavior than set.erase(x).

set.erase(x) will erase duplicate elements, while set.erase(set.find(x)) will erase the first element.

Share this post


Link to post
Share on other sites

set.erase(x) will erase duplicate elements, while set.erase(set.find(x)) will erase the first element.

No, you were right the first time. Elements in a set are unique, so there's no way it could ever erase more than one element. The only difference is that one returns void, while the other returns 0 or 1.


Return value
[font="verdana, arial, helvetica, sans-serif"]Only for the second version, the function returns the number of elements erased, which in [/font]set[font="verdana, arial, helvetica, sans-serif"] containers is 1 if an element with a value of [/font]x[font="verdana, arial, helvetica, sans-serif"] existed (and thus was subsequently erased), and zero otherwise.[/quote][/font]

Share this post


Link to post
Share on other sites

Visual C++ has some iterator debugging it uses in release builds (which you have to explicitly disable if you don't want it). I'm not sure exactly how it's all implemented and whether or not it could've helped in this situation.

MSVC's debug would spit out "map/set erase iterator out of range". I personally haven't had any complaints about 2010's implementation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!