Le 04/11/2012 18:35, Tommi a écrit :
I have a fundamental language design talking point for you. It's not
specific to D. I claim that, most of the time, a programmer cannot, and
shouldn't have to, make the decision of whether to allocate on stack or
heap. For example:
void func(T)()
{
auto t = <allocate T from heap or stack?>
...
}
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.
So, the question of whether to allocate that variable t on stack or heap
is something that only the compiler (or runtime) can answer. Is there
any language where you have the ability to say "allocate T from wherever
it's best"?
I wonder if it would be possible in D to let the compiler allocate
dynamic arrays on stack when it can statically guarantee that it's safe
to do so (no dangling references, never increasing the size of the
array, etc).
BTW, Why can't we allocate 1Gb stack or something insanely big on 64bits
systems ?
It will not use actual physical memory unless it is used, and much more
stuffs can be allocated on stack.