3 ms·
It 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, whic
by azakai 4y ago
It 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.