2 ms·
I wonder what a good approach to safe pointer arithmetic would be.
by jdc 5y ago
I wonder what a good approach to safe pointer arithmetic would be.
- pornel 5y agoRust's actually-safe pointer arithmetic is done via slices (because it's necessary to track the length): slice = &slice[1..]; It compiles to roughly `ptr++; len--;` and a bounds check if necessary. `1234 as *const u8` and `ptr.offset(n)` are also safe, because Rust allows invalid/dangling pointers to exist if they're never dereferenced.
- rectang 5y agoThis answer addresses the letter of jdc's musing, but not the gist. The code that you've supplied may be "safe", but the issue is that there's no way to constrain pointer arithmetic such that the result always points at a valid location which can withstand dereferencing.