News for hackers. Stuff that matters.
Re:Rust Memory Management by falcolas on Tuesday May 09, 2017 @09:53AM (#14299976)
> stack allocation is preferred over the heap allocation and that's where everything is allocated by default

Has this ever been proven to be a problem in practice, given that a program's stack is typically limited in size? Would the recommended workaround (if this was a problem) be to change variables to use heap instead?

Re:Rust Memory Management by dom0 on Tuesday May 09, 2017 @10:06AM (#14300077)
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.

Re:Rust Memory Management by bogomipz on Tuesday May 09, 2017 @10:33AM (#14300317)
>"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.

Re:Rust Memory Management by bogomipz on Tuesday May 09, 2017 @04:03PM (#14303352)
I see, that would make sense. Thanks.
Re:Rust Memory Management by falcolas on Tuesday May 09, 2017 @10:25AM (#14300242)
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.

Re:Rust Memory Management by masklinn on Tuesday May 09, 2017 @10:46AM (#14300445)
> 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...
Re:Rust Memory Management by pjmlp on Tuesday May 09, 2017 @02:31PM (#14302662)
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.
Re:Rust Memory Management by dom0 on Tuesday May 09, 2017 @02:42PM (#14302769)
exclusively GC'd
Re:Rust Memory Management by pjmlp on Tuesday May 09, 2017 @02:52PM (#14302849)
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.
Re:Rust Memory Management by Veedrac on Tuesday May 09, 2017 @12:27PM (#14301381)
They don't allocate objects on the stack, though; they merely store bindings (and, in Java's case, primitives).
Re:Rust Memory Management by pcwalton on Tuesday May 09, 2017 @01:21PM (#14301977)
> Has this ever been proven to be a problem in practice, given that a program's stack is typically limited in size? Would the recommended workaround (if this was a problem) be to change variables to use heap instead?

It is occasionally a problem. For example, when writing my old emulator, I statically allocated the system's RAM (which has a fixed size) on the stack at first before thinking better of it and moving it to the heap. But such situations are uncommon.