0

I'm working on a project (settlers 2.5 return to the roots namely) which suffers some problems. I hope to be able to solve some of them by using only stl containers and bounds checking.

So is it somehow possible to use bounds-checking in STL containers also in release mode? So we could use a (less performing) version, that does them and crashes/throws on error?

The build server uses MinGW to crosscompile from linux to windows and apple.

Edit: []-operator is used exclusively. Changing to .at() would require a large search-and-replace action which got to be reversed at some point. Best would be, if there is a define or something that switches []-behaviour to .at()

Flamefire
  • 5,313
  • 3
  • 35
  • 70

1 Answers1

1

First approach

You can rely on exceptions to verify bound checking using STL container. This is enable in both debug and release mode.

For instance std::vector::at throws an out of range exception if beyond the bounds.

Second approach

If you use operator[] and you do not want to change your code, you can enable _GLIBCXX_DEBUG flag for bound checking.

coincoin
  • 4,595
  • 3
  • 23
  • 47
  • Yes but this requires rewriting almost the whole code. I thought about a flag or something like that. – Flamefire Jul 23 '15 at 15:11
  • So basically can you precise what are you using ? operator[] for instance ? If you have an example code it would be nice. I edited my answer for this case. – coincoin Jul 23 '15 at 15:17
  • Does _GLIBCXX_DEBUG work in MinGW? I read about some issues of containers returning wrong values when this is set... – Flamefire Jul 23 '15 at 16:16
  • I am unaware of any such issues with `_GLIBCXX_DEBUG`, noone has reported them to our bug database. There is a caveat that you must recompile everything with `_GLIBCXX_DEBUG` because it changes the ABI of the containers. – Jonathan Wakely Jul 23 '15 at 16:19
  • It should since this is a port of gcc. I am not aware about the issues. If you have some links. The best way would be to try with some examples :) – coincoin Jul 23 '15 at 16:19