Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to switch from vector::at() to [] when building Release

I find vector::at() useful for alerting against out-of-bounds bugs while debugging, but it’s painfully slow and unsuited for release code. Is there a known compiler flag or some method to automatically convert vector::at() to vector::operator[] when compiling in release mode, sort of like how asserts() are stripped in release with DNDEBUG?

Edit:
Follow the linked question for a solution to this, or check the accepted answer (+ the comments below this question for some compiler specific stuff). Basically, this problem can be solved in reverse (there are options to allow bounds checking for vector::operator[] in debug mode).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want no bounds checking overhead at runtime, then use the subscript operator.

With most standard library implementations, you can enable non-standard bounds checking in subscript operator. Usually by defining a macro. This has a caveat, that the entire program must be compiled in the same mode, including any linked libraries.

Address/UB sanitisers should also be used when testing and debugging, and they may also detect typical buffer overflow conditions in absence of standard library support.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading