Solved

How can I turn on bounds checking in STL vectors?

Posted on 2000-04-17
3
190 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
  • 2
3 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

810 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