?
Solved

How can I turn on bounds checking in STL vectors?

Posted on 2000-04-17
3
Medium Priority
?
205 Views
Last Modified: 2013-12-14
I would like to be able to turn on bounds checking
for debugging and turn it off on production runs.
What is the easiest way to do this with STL vectors.

Thanks,
  Ken
0
Comment
Question by:klopter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 2724301
the vector class wasn't really designed with debug/release modes in mind.  The operator [] function does not have to check the index value (in most impilentations it won't check it) and the at() function always has to check the index value.  

You rbest bet might be to use some conditional compilation code that causes at() to be used in debugging and operator [] to be used in release mode.

continues.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2724316
for example

#ifdef DEBUG
   GetItem(V,P) V.at(P)
#else
   GetItem(V,P) V[P]
#endif

   *    *    *

vector<int> Vec;

int i = GetItem(Vec,5);

I don't really like using the pre-processor to do macros and that short of thing, but that is probalby the only way you can do this.  (Well you coudl write your own array class, from scratch or based on vector<>, but that might be more thn you want.)
0
 

Author Comment

by:klopter
ID: 2724442
Bummer.  One of the reasons that I
agreed to switch from arrays to
vectors was to get bounds checking.
There are undoubtably many other good
reasons to convert though.

And, at leastnow I do have some
options for bounds checking.

Ken
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question