4 ms·
This is awesome! Does Zig also have the memory safety guarantees of Rust? It seems like a much nicer language to learn and use IMO
by no_wizard 4y ago
This is awesome!
Does Zig also have the memory safety guarantees of Rust? It seems like a much nicer language to learn and use IMO
- dralley 4y agoNo, it doesn't make many safety guarantees. The language and tooling is designed to make it easier to write safe code than languages like C, but the compiler doesn't enforce safety. The burden is smaller but it's still the programmer's job.
- azakai 4y agoIt does enforce spatial memory safety, at least, which is a large improvement over C (and to some extent C++). It does not enforce temporal memory safety, which is the harder one. Rust does have that (but it has unsafe blocks due to the limitations), and GC does have that (but it has overhead). Zig might add a temporal memory safety mode in some way eventually, but it will almost certainly add overhead. Example options for that: never release allocated blocks to the OS, or Type-After-Type (different allocation regions per type). Both add enough overhead that it won't be good enough for some use cases, but fine for others.
- eptcyka 4y agoGCs don't enforce temporal safety, no GC will ever prevent a race condition. The fact that the ownership concept in Rust prevents use-after-free bugs is just almost a side-effect of achieving fearless concurrency.
- azakai 4y agoYou're right that Rust also prevents data races (which is great!), but "temporal memory safety" is usually used to mean use-after-free and related issues, not type-safe data races. That was the context here.
- pjmlp 4y agoNeither does Rust prevent all kind of race conditions, only the very special case of data races in the same process memory. Concurrency related races accessing external resources, nope.
- pcwalton 4y agoZig has raw pointers with pointer arithmetic/indexing [1], so as far as I can tell it doesn't enforce spatial memory safety. I don't think spatial memory safety alone is a large improvement over C++ in particular, since most memory safety vulnerabilities nowadays are UAF. For C, maybe; I'd have to look at LazyFishBarrel to see how many bugs nowadays are spatial as opposed to temporal (I'd guess that temporal vulnerabilities are more common nowadays in C too). [1]: https://ziglang.org/documentation/master/#Pointers https://ziglang.org/documentation/master/#Pointers
- azakai 4y agoI'm not an expert on Zig, but my understanding is that it also has safe slices, and pointers which disallow arithmetic, and encourages their usage in idiomatic Zig. I assume that's why this nice post describes Zig as having spatial memory safety: https://www.scattered-thoughts.net/writing/how-safe-is-zig/ https://www.scattered-thoughts.net/writing/how-safe-is-zig/ Agreed that UAF is the larger factor these days for C++. Those are just impossible to really get rid of in such a language, without the overhead of something like Type-After-Type (which in some cases is just too much).
- pjmlp 4y agoWhich Modula-2 and Mesa were already doing in the 70's, or NEWP/ESPOL in the 60's, or JOVIAL in the late 50's. Hence why it would be nice if Zig would be a bit better in that regard.
- geodel 4y agoNo.
- generichuman 4y agoYou can't have Rust's safety guarantees without being at least somewhat similar to Rust. There are attempts to achieve this in a simpler way (such as Vale) but right now you're better off using something with GC if you want that sort of safety.
- deleted 4y ago[deleted]
- pjmlp 4y agoNo, it is similar to Modula-2. Much safer than plain C, but use-after-free and double free will still get you.