Bounds Checking Debate
Discussions focus on bounds checking for array accesses in C, C++, and Rust, debating compile-time vs. runtime checks, compiler optimizations that eliminate them, and performance trade-offs.
Activity Over Time
Top Contributors
Keywords
Sample Comments
What C compiler would add bounds checks?
Yeah, unless you bypassed the bounds checking with unsafe{} for performance reasons. (The is an unlikely place to do so.)
You can't do compile time bounds checking, if that's what you're implying.
How would you enforce bounds checking while supporting unsafe languages?
You can enable bound checking when calling the compiler. I don't remember the actual command line flags, though.
I don't think bounds checking can be done at compile-time, especially in C/C++.
Remind me, how is this a good thing again? Especially considering that (if you write modern C++) the compiler should optimize away bound checks most of the time (and in all critical places) either way.
Rusts bounds checking is done at compile time, so you don't need to turn it off.
Out of curiosity, do you know projects with C code and bounds checks?
It appears the compiler optimizes out bounds checking if and only if it can tell during compile time that the index is correct [1]. Arrays of dynamic size or with dynamic references are bound checked. In dynamic cases you should be writing manual bound checking guards in C anyway.[1]: https://github.com/mozilla/rust/issues/9024