4 ms·
Ensuring memory safety in C++ is an endless game of spinning plates. It's possible, but the "best practices" for doing so certainly don't feel like best practic
by smoldesu 5y ago
Ensuring memory safety in C++ is an endless game of spinning plates. It's possible, but the "best practices" for doing so certainly don't feel like best practices. I've watched people repeatedly compile a program, changing a single line and running it through Valgrind for hours on end. With Rust, you actively have to fight the compiler to cause memory leaks.
Addendum: yes, that last line is what pisses a lot of people off with Rust, and honestly, rightfully so. I've written quite a bit of Rust by now, and getting your code to work can feel like a bit of an escape room. Having C as an option on the kernel alongside it is a great idea though, because there are situations where I'd happily pick up C over Rust, and they're not just 'edge cases' where Rust falls short. Ideally, they're two different tools that can be applied depending on the situation at hand.
- mgaunard 5y agoIf you correctly model ownership with RAII, which also enforces strong class invariants, and make sure all operations satisfy basic exception safety, then leaks are not possible. Those are the most elementary rules that one must follow when writing C++.
- bcrosby95 5y ago> If you correctly model ownership Doing something correctly is not a best practice though. If it were, we could just say "if you correctly manage memory". What happens when you mess up? What happens if you incorrectly model ownership with RAII? Or is it not really possible?
- deleted 5y ago[deleted]
- int_19h 5y agoObject graphs where there's no clear owner are not automatically "incorrect" - whatever they're modelling might be non-hierarchical itself - yet would result in a leak without a GC.
- mgaunard 5y agoThey are, sorry. There is no way to enforce basic guarantees without ownership.
- int_19h 5y agoGraphs where no single object is a clear owner happen all the time in real world, so I would argue that any object model that can't accurately model that is just not good enough. And what basic guarantees are not enforceable without ownership?
- woodruffw 5y agoOne nit: > With Rust, you actively have to fight the compiler to cause memory leaks. Memory leaks are not considered unsafe in Rust[1]! They're actually an important part of Rust's lifetime semantics: intentionally leaking memory is both safe and equivalent to producing a `'static` out of thin air. You probably shouldn't leak memory as a matter of practice, of course, but Rust will happily allow you to do so in 100% safe code. [1]: https://stackoverflow.com/a/56108074 https://stackoverflow.com/a/56108074