>"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?
Re:Rust Memory Management
by paupino_masano on Tuesday May 09, 2017 @10:44AM
(#14300431)
C# 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).
Re:Rust Memory Management
by DorothySim on Tuesday May 09, 2017 @10:49AM
(#14300470)
> 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.
I see, that would make sense. Thanks.
Sure, 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.
> 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.
Re:Rust Memory Management
by steveklabnik on Tuesday May 09, 2017 @10:51AM
(#14300488)
The second edition of the book goes into it in great detail https://doc.rust-lang.org/nightly/book/second-edition/ch04-0...
Languages like D, Oberon or Modula-3, just to name three out of many, do have GC and also support explicit global and stack allocation.
exclusively GC'd
I saw that, but wasn't sure how you actually meant it.
Re:Rust Memory Management
by johncolanduoni on Tuesday May 09, 2017 @11:09AM
(#14300673)
Java 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.
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.