47

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

Mr Fooz
  • 109,094
  • 6
  • 73
  • 101
pic11
  • 14,267
  • 21
  • 83
  • 119

2 Answers2

76

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
  • 112,025
  • 15
  • 165
  • 265
Peter G.
  • 14,786
  • 7
  • 57
  • 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
  • 61,704
  • 67
  • 242
  • 415