4 ms·
I would not even say that Rust (or any other language) is or can be perfectly safe in "safe" code even with a bug-free compiler. It is possible to access unallo
by robalni 6y ago
I would not even say that Rust (or any other language) is or can be perfectly safe in "safe" code even with a bug-free compiler.
It is possible to access unallocated memory in 100% "safe" Rust, because what memory that is allocated and not depends on the programmer's intent.
Allocated memory is memory that you intend to use.
If I make an array of 3 integers that are not supposed to be used yet but I accidentaly access one of them, then that is a memory-safety bug because I accessed unallocated memory.
The functions that languages provide to "allocate" memory don't define what allocating memory means.
They just provide a tool that you can use to keep track of what memory you are using.
What memory is allocated or not is relative and there are multiple levels of allocation.
If we zoom out a bit we can even call C a memory-safe language because all memory accesses in C must be to allocated memory and the ones that are not will kill the process
(allocated here as in allocated to the process by the operating system).
- saagarjha 6y agoI am fairly sure such constructs require unsafe? https://doc.rust-lang.org/std/mem/union.MaybeUninit.html https://doc.rust-lang.org/std/mem/union.MaybeUninit.html
- robalni 6y agoNo, I'm just saying that what memory is allocated (as in the language) is not necessarily the same as the memory that is allocated as in your intentions. It's possible to allocate (as in language) an array that is longer than what you really need, and when you are done using some of the memory in that array you might not want to call any free/realloc functions in order to make the program faster. So instead you might want to just remember that the memory is unallocated (as in intentions) even if it's still allocated (as in language). And now there is a disagreement between you and the language on what memory is allocated. No "memory-safe" language can make sure you don't accidently use memory unintentionally. With that said, languages like Rust really give you helpful tools to minimize the unintentional memory accesses.
- saagarjha 6y agoOh, I guess that is fair.