3 ms·
The most important of these language facilities, IMO, is Rust's "fat pointers". In Rust, a "&[T]" is not a single word but two: the first word is a pointer to t
by cesarb 8y ago
The most important of these language facilities, IMO, is Rust's "fat pointers". In Rust, a "&[T]" is not a single word but two: the first word is a pointer to the first element like in C, but the second word is the number of elements. This keeps both together all the time; you don't normally have the pointer separated from the number of elements (although you can separate them if you really want to), which is what allows the compiler to insert the bounds checks.
(Another interesting use of fat pointers is in Rust's "trait objects", where the first word of a "&Trait" is a pointer to the object, and the second word is a pointer to the vtable. In C++, the pointer to the vtable is part of the object; in Rust, it's part of the reference.)
- greglindahl 8y agoDebugging C compilers have been using "fat pointer" implementations for decades, to do bounds checking, but of course it's dangerous to use that into C code that assumes things about the sizes of pointers. Fortran added array descriptors in 1990, when it made arrays first class objects. That was a quite visible language change. So this is a well-studied area with lots of implementations you can look at.