473,804 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Catching vector index out of bounds

Is there a common way to check for vector indexes being in bounds? My
version of the STL has no such check, even in debug builds. I was
considering deriving a class from Vector with its own operator[], and using
that in my code instead of std::vector. Then I'd use an #ifdef _DEBUG
switch which typedef'ed my vector class name to std::vector if in release or
used the derived class if in debug. Does that sound reasonable? Is there
an easier and more common way?
Jul 23 '05 #1
4 5799
"Biff" <fk**@fcvie.com > wrote in message
news:36******** *****@individua l.net...
Is there a common way to check for vector indexes being in bounds? My
version of the STL has no such check, even in debug builds. I was
considering deriving a class from Vector with its own operator[], and
using that in my code instead of std::vector. Then I'd use an #ifdef
_DEBUG switch which typedef'ed my vector class name to std::vector if in
release or used the derived class if in debug. Does that sound
reasonable? Is there an easier and more common way?


Look at the function "at" which should already be in your std::vector. I
think it does what you want.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #2

"Biff" <fk**@fcvie.com > wrote in message
news:36******** *****@individua l.net...
Is there a common way to check for vector indexes being in bounds? My
version of the STL has no such check, even in debug builds. I was
considering deriving a class from Vector with its own operator[], and using that in my code instead of std::vector. Then I'd use an #ifdef _DEBUG
switch which typedef'ed my vector class name to std::vector if in release or used the derived class if in debug. Does that sound reasonable? Is there
an easier and more common way?


std::vector::at ()

The argument is the same you'd use for operator[](),
but if given an out-of-bounds value, throws an exception
(of type 'std::out_of_ra nge').

-Mike
Jul 23 '05 #3
> Is there a common way to check for vector indexes being in bounds? My
version of the STL has no such check, even in debug builds. I was
considering deriving a class from Vector with its own operator[], and
using that in my code instead of std::vector. Then I'd use an #ifdef
_DEBUG switch which typedef'ed my vector class name to std::vector if in
release or used the derived class if in debug. Does that sound
reasonable? Is there an easier and more common way?


Use member function at()

I sometimes use the following for vector and deque:

#if defined(_DEBUG)
#define AT(x) at(x)
#else
#define AT(x) operator[](x)
#endif

and then have something like

std::vector<int > v;

int i = v.AT(0);

For debug builds, access is ranged-checked
For release builds, access is not ranged-checked

Of course there is nothing to stop you from doing

int j = v[1];
int k = v.at(2);

when you defiintely want no range checking or range checking.

Stephen Howe
Jul 23 '05 #4
Biff wrote:
Is there a common way to check for vector indexes being in bounds?
You could use member function

reference
at(size_type __n) { _M_range_check( __n);
return (*this)[__n]; }
const_reference
at(size_type __n) const { _M_range_check( __n);
return (*this)[__n]; }

but that throws an exception and is *not* appropriate
if you are trying to trap programming errors (bugs).
My version of the STL has no such check, even in debug builds.
I was considering deriving a class from Vector with its own operator[]
and using that in my code instead of std::vector.
Then I'd use an #ifdef _DEBUG switch
which typedef'ed my vector class name to std::vector
if in release or used the derived class if in debug.
Does that sound reasonable?
It sounds very reasonable.
Is there an easier and more common way?

My GNU C++ compiler defines members:

reference
operator[](size_type __n) { return *(begin() + __n); }

const_reference
operator[](size_type __n) const { return *(begin() + __n); }

in /usr/include/c++/3.4.0/bits/stl_vector.h
You could redefine them:

reference
operator[](size_type __n) {
assert(__n < this->size()); return *(begin() + __n); }

const_reference
operator[](size_type __n) const {
assert(__n < this->size()); return *(begin() + __n); }

Anyway, you should check your implementation.
You might find that this has already been done for you.
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2882
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
17
25169
by: Havatcha | last post by:
Does anyone have a benchmark for the processing overhead of the STL Vector class, vs a C style array? I would dearly love to use Vectors, but am paranoid about slowing my real-time code down. Can anyone reassure?
3
6736
by: Daniel J Watkins | last post by:
Hi, Some runtime memory exceptions are being exhibited with some code I've written. Can you clarify the following with you to see if my understanding of the principles under question are correct. I'm trying to create a data structure that will allow me to have a dynamic number of columns and a dynamic number of rows in each column (there may be ten rows in column one and twenty rows in column two for example). i. I can declare...
1
2028
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object" occasionally when adding a new element to a vector. By
0
1349
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object" occasionally when adding a new element to a vector. By
18
2578
by: imutate | last post by:
I have an integer variable and I am testing it as follows #define NULL_VAL -1 ... if (x.id != NULL_VAL) { std::cout << x.id << std::endl; ... }
3
2247
by: n.torrey.pines | last post by:
I'd like to be able to view two contiguous elements of a vector as a pair. Assuming I'm not accessing the last element, of course, and the element type is not bool, when is it safe to do so, from the language definition point of view? const pr& p = *(const pr*)(&v); // pr - either std::pair or hand-defined pair of elements
5
2731
by: Boltar | last post by:
Hi Is there a way of inserting and erasing to/from a vector using an array index instead of an iterator or alternatively a way to map an index number to an iterator? eg: vector<intv;
3
5660
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t k = i+1;
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.