Discussion:
bounds checking in vector class
(too old to reply)
Bae, Hyun-jik
2004-05-11 12:32:50 UTC
Permalink
I found that MFC CArray does boundschecking by ASSERT(0<=index &&
index<length); but STL vector class doesn't.

I prefer using STL vector. Is there any way to enable boundschecking in STL?
I use visual c++ 2003 with MFC and not .NET.

Thanks in advance.

Bae, Hyun-jik
Jerry Coffin
2004-05-11 13:40:11 UTC
Permalink
Post by Bae, Hyun-jik
I found that MFC CArray does boundschecking by ASSERT(0<=index &&
index<length); but STL vector class doesn't.
I prefer using STL vector. Is there any way to enable boundschecking in STL?
I use visual c++ 2003 with MFC and not .NET.
std::vector::at() does bounds checking.
--
Later,
Jerry.

The universe is a figment of its own imagination.
tom_usenet
2004-05-11 16:10:30 UTC
Permalink
On Tue, 11 May 2004 21:32:50 +0900, "Bae, Hyun-jik"
Post by Bae, Hyun-jik
I found that MFC CArray does boundschecking by ASSERT(0<=index &&
index<length); but STL vector class doesn't.
I prefer using STL vector. Is there any way to enable boundschecking in STL?
I use visual c++ 2003 with MFC and not .NET.
Thanks in advance.
If you buy the full Dinkumware library from
http://www.dinkumware.com/libdual_vc.html you can use their "safe
iterator" mode which detects a lot of problems, including (I suspect),
out of bounds in vector::operator[]. Alternatively, you could hack the
vector header to add an assert in operator[] (which should be their
anyway IMHO - it gives "nice" undefined behaviour for debug modes
while avoiding compromising performance when not debugging), or you
could use vector::at (and remember to catch the exception!)

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
P.J. Plauger
2004-05-11 17:01:27 UTC
Permalink
Post by tom_usenet
On Tue, 11 May 2004 21:32:50 +0900, "Bae, Hyun-jik"
Post by Bae, Hyun-jik
I found that MFC CArray does boundschecking by ASSERT(0<=index &&
index<length); but STL vector class doesn't.
I prefer using STL vector. Is there any way to enable boundschecking in STL?
I use visual c++ 2003 with MFC and not .NET.
Thanks in advance.
If you buy the full Dinkumware library from
http://www.dinkumware.com/libdual_vc.html you can use their "safe
iterator" mode which detects a lot of problems, including (I suspect),
out of bounds in vector::operator[]. Alternatively, you could hack the
vector header to add an assert in operator[] (which should be their
anyway IMHO - it gives "nice" undefined behaviour for debug modes
while avoiding compromising performance when not debugging), or you
could use vector::at (and remember to catch the exception!)
Right. Or just change operator[] to call at. Safe iterator does much
more than check accesses, BTW. It ensures that an iterator that
should point into a container actually does so, and hasn't been
invalidated by an earlier operation on the container. It even checks
that sequences that should be ordered by some predicate actually are,
and that the predicate truly enforces a strict weak ordering. You
might be surprised at how hard it is to find bugs caused by such
lapses.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com

Loading...