On 11/4/2012 9:35 AM, Tommi wrote:
The question of whether variable t should lay in heap or stack, depends not only on the sizeof(T), but also on the context in which func is called at. If func is called at a context in which allocating T on stack would cause a stack overflow, then t should be allocated from the heap. On the other hand, if func is called at a context where T still fits nicely on the stack, it would probably be better and faster to allocate t on stack.
You can't determine this, even at runtime, because you don't know what subsequent calls will use enough stack to overflow it.
It is true, though, that you can do what is called "escape analysis", which if it can prove that a reference to the object cannot escape the function scope, then it can be allocated on the stack (if it isn't larger than some arbitrarily chosen size).
Such escape analysis is entirely possible with D without changing any semantics.
