6 ms·
> null terminated strings feel like idiomatic C to most people Doesn't that mean that null terminated strings is idiomatic C? That is, my understanding of the
by michaelmior 6y ago
> null terminated strings feel like idiomatic C to most people
Doesn't that mean that null terminated strings is idiomatic C? That is, my understanding of the term idiomatic is that it is defined by whatever is most natural to users of a language regardless of whether it is the most performant.
- rcoveson 6y agoNull terminated strings are often called cstrings. They’re beyond idiomatic; they’re part of the C standard library.
- lambda 6y agoNot just the standard library, but the core language. They are what you get if you write a string literal in your code.
- jacoblambda 6y agoYes but if we are being pedantic no. Of course the language does still greatly lean into the null terminated string concept. You get a null terminated char array but the length is actually available since arrays (as long as they haven't decayed into pointers) can still share their length at compile time.
- akkartik 6y agoAlso Unix. Syscalls return null-terminated strings all over the place. So any language running on Unix has to deal with null-terminated strings. I know because I run a userland that uses length-prefixed strings as far as possible: https://github.com/akkartik/mu https://github.com/akkartik/mu
- michaelmior 6y agoTrue. I was skipping over that, but the fact that functions manipulating null terminated strings are part of the standard library is certainly a reason in itself to consider them idiomatic.
- akkartik 6y agoThey're agreeing with you.
- michaelmior 6y agoYes, I understand :)
- josephg 6y agoOne of my long standing complaints about modern programming is how rarely people read code. We don't encourage it in school, and in the workplace most people only read code written by their coworkers. It would be the equivalent of teaching people to write books without encouraging them to read anything. To break myself of the habit I started reading some well regarded programs for fun. And oh boy, have I learned a lot from doing so. One of my first discoveries was this beauty in the Redis source code: https://github.com/redis/redis/blob/3.0/src/sds.h https://github.com/redis/redis/blob/3.0/src/sds.h The idea is to have a string struct that stores its length and content. But the pointer passed around is a pointer to the (null terminated) contents field in the struct. The string is efficient for internal calls (the length can be queried by subtracting from the pointer). But the pointer is also an idiomatic null-terminated C string pointer, compatible with the standard library and everything else. (typedef char *sds;) Dovecot is also a gem to read if you're looking for inspiration. The way it manages memory pools is delightful - and I'm sure much more performant than idiomatic rust. (That is, without reaching for arena allocator crates and alternate implementations of Box and Vec).
- steveklabnik 6y agoWhat makes you think arenas are considered unidiomatic in Rust? They’re there to be used when appropriate!
- josephg 6y agoThey seem unidiomatic because you have to fight the standard library to use them. Using an arena means abandoning String, Vec, Box, std::collections, and so on. I have no problem doing that if I need to. But it feels like I'm fighting against the grain of rust more than I'd like.
- burnthrow 6y agoThis topic is of great interest to me, do you know if there is any related official or community documentation on using arena allocation (and the problems you mentioned) in Rust? I've only found https://doc.rust-lang.org/1.1.0/arena/index.html https://doc.rust-lang.org/1.1.0/arena/index.html