3 ms·
I've got a simple way of implementing Rust's borrow checker in C. It's based on always nulling out old pointers rather than letting them dangle. Functions that
by btrask 10y ago
I've got a simple way of implementing Rust's borrow checker in C. It's based on always nulling out old pointers rather than letting them dangle. Functions that take ownership of an argument demand a pointer-to-pointer, and then null out the caller's copy. When freeing memory which might've contained pointers, I assert that the entire thing is zeroed, to ensure it doesn't have any dangling pointers or leaks. Non-owning pointers are marked with const so they can't be freed.
This obviously tracks ownership at runtime, rather than compile time, but static analyzers are pretty good at tracking nulls, and optimizers minimize the runtime performance impact. The end result is very similar to Rust, albeit coming from the other direction.
At some point I'd like to write up a blog post with examples.