Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Note that STL containers allow you to bounds check "[]" indexing even in release mode if you set the right preprocessor directives. The flag differs between compilers and stdlib implementations, though. For libstdc++ (default on linux), use _GLIBCXX_ASSERTIONS. For example, the following code aborts on release builds too:

  // g++ -O3 -D_GLIBCXX_ASSERTIONS=1 file.cpp
  #include <iostream>
  #include <vector>

  int main() {
    std::vector<int> vec{1, 2, 3, 4};
    std::cout << vec[4];  
  }
MSVC provides similar functionality using the _ITERATOR_DEBUG_LEVEL directive. The point is that there is no inherent limitation that one has to use the .at() method for bounds checking STL container access.


Good to know, thanks!




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: