3

Possible Duplicate:
GCC STL bound checking

Is there something like a automatic debug version of the std:vector in the g++ STL and if not how could I achive one?

What I want is to specify a debug parameter in my g++ call for example:

g++ -D DEBUG_ main.cpp

When this parameter is defined I want all my std::vectors to check their boundarys when accesing an element, the way when using vector::at().

When the parameter is omitted I want all vectors to behave as if the normal [] operator is used, meaning no performance is "wasted" for boundary checking.

I heard that VC++ does something like this. So my question is how to to this using the g++?

Community
  • 1
  • 1
Haatschii
  • 9,021
  • 10
  • 58
  • 95

2 Answers2

8

The flag you want is -D_GLIBCXX_DEBUG

More debug options for libstdc++ can be found at:

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

vkrnt
  • 155
  • 1
  • 5
Dave S
  • 20,507
  • 3
  • 48
  • 68
0

in debug mode you can have additional assertion and checking, but using at() or [] it's always on your side.

at() - throws out_of_bounds exception, [] - don't

triclosan
  • 5,578
  • 6
  • 26
  • 50