4 ms·
std::vector [] has had bounds checking since forever if you set the correct compiler flag. Since they aren't using it this is a choice, presumably they prefer t
by TinkersW 2y ago
std::vector [] has had bounds checking since forever if you set the correct compiler flag. Since they aren't using it this is a choice, presumably they prefer the speed gain.
- mike_hearn 2y agoYou mean _GLIBCXX_DEBUG? It's got some issues. Linux only, it doesn't always work [1] and it's all or nothing. What's really needed is the ability to selectively opt-out on a per-instantiation level so very hot paths can keep the needed performance whilst all the rest gets opted into safety checks. Microsoft has this: https://learn.microsoft.com/en-us/cpp/standard-library/safe-libraries-cpp-standard-library?view=msvc-170 https://learn.microsoft.com/en-us/cpp/standard-library/safe-... but it doesn't seem to actually make std::vector[] safe. It's frustrating that low hanging fruit like this doesn't get harvested. [1] "although there are precondition checks for some string operations, e.g. operator[], they will not always be run when using the char and wchar_t specializations (std::string and std::wstring)."
- TinkersW 2y agoWith MSVC you can use _CONTAINER_DEBUG_LEVEL=1 to get a fast bounds check that can be used in release builds. Or just use it in development to catch errors.
- mike_hearn 2y agoInteresting thanks. Seems the reason I couldn't find anything on that is because it's internal only and not a feature you're actually meant to use? https://github.com/microsoft/STL/issues/586 https://github.com/microsoft/STL/issues/586 > We talked about this at the weekly maintainer meeting and decided that we're not comfortable enough with the (lack of) design of this feature to begin documenting it for wide usage.
- pjmlp 2y agoWhat you want should be _ITERATOR_DEBUG_LEVEL instead, that is the public macro for bounds checking configuration.
- Calavar 2y agoAs far as I am aware, the standard doesn't mandate bounds checking for std::vector::operator[] and probably never will for backwards compatibility reasons. Most standard library implementations have opt-out std::vector[] bounds checking in unoptimized builds, but not in optimized builds. I tried a toy example with GCC [1], Clang [2], and MSVC [3], and none of them emit bounds checks with basic optimization flags. [1] https://godbolt.org/z/W5e3n5oWM https://godbolt.org/z/W5e3n5oWM [2] https://godbolt.org/z/Pe8nPPvEd https://godbolt.org/z/Pe8nPPvEd [3] https://godbolt.org/z/YTdv3nabn https://godbolt.org/z/YTdv3nabn
- TinkersW 2y agoAs I said you need the correct flag set.. MSVC use _CONTAINER_DEBUG_LEVEL=1 and it can be used in release. They have had this feature since 2010 or so, though the flag name has changed.
- pjmlp 2y agoThe correct name is _ITERATOR_DEBUG_LEVEL.
- pjmlp 2y agoAdd a "#define _ITERATOR_DEBUG_LEVEL 1" on top for VC++.