Mixing safe and unsafe
Mixing safe and unsafe
Posted Oct 30, 2025 9:20 UTC (Thu) by matthias (subscriber, #94967)In reply to: Mixing safe and unsafe by tialaramex
Parent article: Fil-C: A memory-safe C implementation
Thanks. I somehow missed that.
>You're wrong about the special-ness of one-past-the-end in Rust. It's not special in Rust, it's just one past the end.
The provenance documentation says it is different from 16 past the end, as it is still inside provenance. Just the same as in C.
> If you're thinking "I would use a dereference" Bzzt, that's going to be a problem. unsafely *ptr = some_goose; will try to destroy the previous goose, but there is no goose, just uninitialized memory so that's UB.
Only if goose implements drop. But then you are (implicitly) creating a &mut to the uninitialized memory, which is indeed UB. And usually the drop handler will read the memory, which is also UB. This is not really a difference in pointer semantics but more a difference on how the assignment operator works. I do agree that ptr.write() should be used, if there is no valid object that you want to drop, even if the type does not implement drop. It is much more obvious that the programmer wants to do a write and not an assignment this way.
Of course there are differences in pointer handling between the languages. I think of pointer comparisions which can be UB in C(++), while they are part of safe rust and thus must not cause any UB. These are details that of course need to be accounted for when writing actual code. However, when thinking of raw pointers in rust, they are much more similar to C pointers than to anything else in the rust language. They always feel somhow alien in the rust language. So having an expressive API with methods like offset and write is a good thing. Of course, it is still unsafe, but less error prone.
