Solved

How can I turn on bounds checking in STL vectors?

Posted on 2000-04-17
3
188 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now