this post was submitted on 25 Jan 2024
22 points (78.9% liked)
C Programming Language
993 readers
1 users here now
Welcome to the C community!
C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.
- irc: #c
π https://en.cppreference.com/w/c
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Correction:
str
is not really stack-allocated, it rather is a fat pointer (i.e. pointer + length) to a string somewhere (on the heap or in the binary).Edith: replaced heap with stack
I never said str is heap-allocated. I'm presuming you meant stack when you said heap (or you meant String when you said str)?
You're right, I meant stack.
str
is not like anu8
array, but a pointer.That's fair. Because I explicitly mentioned &'static str later on, my explanation of &str implicitly assumes that it's a non-static lifetime str, so it isn't stored in the executable, which would only leave the stack. I didn't want to get into lifetimes in what's supposed to be a high-level description of types for non-Rust programmers, though. I mentioned 'stack' and 'heap' explicitly here because people understand that they mean 'fast' and 'slow', respectively. Otherwise the first question out of people's mouths is 'why have a non-growable string type at all??'.