6 ms·
Stack allocation is the default in all practically used languages that actually have a stack (e.g. C, C++), and heap allocation is explicit in all of those as w
by dom0 9y ago
Stack allocation is the default in all practically used languages that actually have a stack (e.g. C, C++), and heap allocation is explicit in all of those as well.
Allocation more or less everything on the heap (i.e. using the stack only for calls and returns) is something that's done out of necessity in exclusively GC'd languages, like Java or Python.
- falcolas 9y agoSure, it's the default, but C and C++ encourage have patterns where calls to `malloc` and `new` much more common than in Rust. Especially since strings are almost always handled as malloc'ed buffers. I'm just wondering if the extra emphasis on stack allocation has created issues; this article doesn't cover the allocation of Strings or Vecs (wrapping them in RC and ARC boxes), and most other references on the net are a few years old now.
- deleted 9y ago[deleted]
- masklinn 9y ago> I'm just wondering if the extra emphasis on stack allocation has created issues; this article doesn't cover the allocation of Strings or Vecs As in C++, these are small stack structures (a few words, IIRC it's 3 for Vec, and String is backed by a Vec) pointing to a heap-allocated buffer.
- steveklabnik 9y agoThe second edition of the book goes into it in great detail https://doc.rust-lang.org/nightly/book/second-edition/ch04-01-what-is-ownership.html https://doc.rust-lang.org/nightly/book/second-edition/ch04-0...
- bogomipz 9y ago>"Stack allocation is the default in all practically used languages that actually have a stack (e.g. C, C++), and heap allocation is explicit in all of those as well." That was my first thought as well when I read that passage. Do you know why the author might have felt the need to state this specific in the context of Rust then?
- paupino_masano 9y agoC# and Java allocate objects to the heap and primitive types to the stack by default so perhaps it may be in context to that (i.e. It's not explicit but rather implicit on the type being used).
- DorothySim 9y ago> C# and Java allocate objects to the heap and primitive types to the stack by default (...) Note that it only applies to local variables. If you have a primitive inside an object then it's allocated on the heap as part of the outer object.
- bogomipz 9y agoI see, that would make sense. Thanks.
- johncolanduoni 9y agoJava uses the stack for local variables (including pointers to objects on the heap and primitives), and I believe Python uses a stack for the same as well. Return pointers are the least of stack usage.
- Veedrac 9y agoThey don't allocate objects on the stack, though; they merely store bindings (and, in Java's case, primitives).
- pjmlp 9y agoLanguages like D, Oberon or Modula-3, just to name three out of many, do have GC and also support explicit global and stack allocation.