Solved

How can I turn on bounds checking in STL vectors?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 119
Problem with SqlConnection 4 160
How can I build my own IDE using ASP.NET MVC? 2 33
oracle 11g 23 50
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now