Solved

How can I turn on bounds checking in STL vectors?

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

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: …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

733 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