45

How do I enable bound checking for operator[] and iterators?

Mr Fooz
  • 103,571
  • 5
  • 68
  • 100
pic11
  • 13,366
  • 17
  • 76
  • 110

2 Answers2

72

You can activate runtime iterator and bounds checking by compiling with -D_GLIBCXX_DEBUG. Also note that random-access containers provide the always bounds-checking at()-operation in addition to operator [].

References:

GCC STL debug mode: http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode_using.html#debug_mode.using.mode

at() operation: std::vector::at(), std::deque::at() and std::array::at()

6502
  • 108,604
  • 15
  • 155
  • 257
Peter G.
  • 14,328
  • 7
  • 55
  • 75
1

you should overload the operator[] for your specific classes. If you want to use an existing STL container, the at() function is a bounds-checked version of the operator[].

Tony The Lion
  • 59,704
  • 62
  • 233
  • 403