Catching vector index out of bounds

Discussion in 'C++' started by Biff, Jan 30, 2005.

  1. Biff

    Biff Guest

    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?
    Biff, Jan 30, 2005
    #1
    1. Advertising

  2. Biff

    Cy Edmunds Guest

    "Biff" <> wrote in message
    news:...
    > 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/
    Cy Edmunds, Jan 30, 2005
    #2
    1. Advertising

  3. Biff

    Mike Wahler Guest

    "Biff" <> wrote in message
    news:...
    > 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::eek:ut_of_range').

    -Mike
    Mike Wahler, Jan 30, 2005
    #3
  4. Biff

    Stephen Howe Guest

    > 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
    Stephen Howe, Jan 31, 2005
    #4
  5. 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.
    E. Robert Tisdale, Jan 31, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZHdlbndhQGNvbXBhbnlhYmMuY29t?=

    Re: Index Out of Bounds Error

    =?Utf-8?B?ZHdlbndhQGNvbXBhbnlhYmMuY29t?=, Oct 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    484
    =?Utf-8?B?ZHdlbndhQGNvbXBhbnlhYmMuY29t?=
    Oct 20, 2004
  2. Mark Olbert
    Replies:
    1
    Views:
    442
    Luke Zhang [MSFT]
    Jan 25, 2006
  3. Method Man

    Index out of bounds question

    Method Man, Oct 14, 2004, in forum: C Programming
    Replies:
    26
    Views:
    633
    Dan Pop
    Oct 22, 2004
  4. Replies:
    7
    Views:
    517
  5. Joel Finkel

    DataGrid1.DataKeys[e.Item.ItemIndex] array index out of bounds

    Joel Finkel, Sep 4, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    303
    Joel Finkel
    Sep 4, 2003
Loading...

Share This Page