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 do I get an error from `pop_back()` if `size()` is 0?

I was explaining to a coworker why we have small test with sanitizers on them. He asked about popping a vector too many times and if it was an exception, assert, UB and which sanitizer catches it

It appears NONE catches them. Address and memory will if you call back() after popping too many times but if you pop and do size() you can get negative values

Is there a way I can get an assert or exception or runtime termination when I pop too many times? I really thought a debug build without sanitizers would have caught that (with an assert or exception)

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

I use clang sanitizer but build options with gcc will also be helpful

>Solution :

Both libstdc++ and libc++ have a "debug mode" with assertions, that can be enabled using:

  • -D_GLIBCXX_DEBUG for libstdc++
  • -D_LIBCPP_DEBUG for libc++

Also -fsanitize=undefined appears to catch it, but the error message is much more cryptic.

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