4 ms·
D slices are fat pointers as well: https://dlang.org/spec/arrays.html#dynamic-arrays https://dlang.org/spec/arrays.html#dynamic-arrays My suggestion for C to
by WalterBright 7y ago
D slices are fat pointers as well:
https://dlang.org/spec/arrays.html#dynamic-arrays https://dlang.org/spec/arrays.html#dynamic-arrays
My suggestion for C to adopt fat pointers:
https://www.digitalmars.com/articles/b44.html https://www.digitalmars.com/articles/b44.html
- mhh__ 7y agoA chaotic part of me thinks that C should be illegal for security code for the lack of this.
- aitchnyu 7y agoJust Googled and seems fat pointers can at least prevent buffer overflow. I want to know more about other returns and costs on changing C to fat pointers. https://security.stackexchange.com/questions/155572/how-are-fat-pointers-a-good-protection https://security.stackexchange.com/questions/155572/how-are-...
- simias 7y agoThey can prevent some buffer overflows, assuming that the data in the fat pointer remains correct. If you want to prevent buffer overflows completely you need to go well beyond that and effectively either use a higher-level language with a runtime and GC making sure everything remains coherent or do what Rust is doing with its borrow checker. Not that I'm against fat pointers in C, I always thought that in hindsight not having a primitive "slice" type was a mistake. In particular using NUL-terminated strings instead of slices is both error-prone and makes implementing even basic string-splitting functions like strtok annoying or inefficient (you need to be able to mutate the source string to insert the NULs or you have to allocate memory and copy each component. If you had slices you could easily return a portion of the string without mutation or copy).
- codedokode 7y ago> If you had slices you could easily return a portion of the string without mutation or copy). In this case you get another problem: how to free memory when there might be many slices pointing at it.
- skrebbel 7y agoDid people in the C community consider your proposal?
- WalterBright 7y agoI haven't officially proposed it, but every time I've shown it to C people they're just silent - no response. But anyway, you can use D in BetterC mode and have bounds checked slices! https://dlang.org/spec/betterc.html https://dlang.org/spec/betterc.html
- kryptiskt 7y agoYou might be interested in this paper: "CheriABI: enforcing valid pointer provenance and minimizing pointer privilege in the POSIX C run-time environment" https://blog.acolyer.org/2019/05/28/cheri-abi/ https://blog.acolyer.org/2019/05/28/cheri-abi/
- x0re4x 7y agoI would interpret the silence of C people as "Thanks, but no, thanks." The success of C for such long time without having fat pointers, exceptions, RAII, etc... is probably an indication they weren't needed that much. (Jonathan Blow: Ideas about a new programming language for games.) https://www.youtube.com/watch?v=TH9VCN6UkyQ&list=PLmV5I2fxaiCKfxMBrNsU1kgKJXD3PkyxO https://www.youtube.com/watch?v=TH9VCN6UkyQ&list=PLmV5I2fxai...
- masklinn 7y ago> D slices are fat pointers as well: So are Rust's. Possibly more interestingly, so are its trait objects, the trait object pointer is a (vtable, instance) rather than (instance*, length). Also of note: that applies to "owned pointers" (`Box`) not just "references", despite the former looking very struct-like: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a0d4e151acba3b8f273666946613f789 https://play.rust-lang.org/?version=stable&mode=debug&editio... In a way, C++ and Rust (and possibly D?) have pretty much an infinity of fat pointers as anything can be made to deref' to something else, and be more than one pointer wide (e.g. C++'s std::string and std::vector and Rust's String and Vec)
- ainar-g 7y ago> Possibly more interestingly, so are its trait objects, the trait object pointer is a (vtable, > instance) rather than (instance*, length). The Go interfaces are the same. See https://research.swtch.com/interfaces https://research.swtch.com/interfaces.
- ummonk 7y agoI mean, some types of implementation of fat pointers are fully compatible with the C spec, and would mean that undefined behavior will result in a crash rather than overflow. Slice behavior would not exist though.
- Crinus 7y agoHow does extern void foo(char a[..]); differ from extern void foo(size_t dim, char a[dim]); if they are meant to be binary compatible?
- pjmlp 7y agoThe programmer doesn't have to keep both in sync manually, so there is less chance that they blow up, as it happens regularly in C code, even after being reviewed.