Variable-length stack allocated arrays

bearophile bearophileHUGS at lycos.com
Tue Jan 12 01:18:12 PST 2010


Walter Bright:
> One of the problems is they can easily lead to stack exhaustion. Stack 
> sizes for a thread must all be preallocated.

Yes, that's of course a problem. It's meaningful to allocate arrays on the stack only if they are small. You can exhaust the/a stack even with normal fixed-sized arrays too:

int foo(int n) {
  int[100] a;
  ...
  y = foo(k);
  ...
}

The main difference is that here you can see better that each nested call to foo() burns 400+ bytes of stack (but as in the variable-length case, it's possible that you don't statically know how many nested calls to foo() will happen at runtime, so the end result is the same: you don't know if you will have a stack overflow at runtime).
In truth we can live without variable-length stack allocated arrays, mine may be just feature envy, and I prefer variable-length stack allocated arrays over the raw alloca().
Thank you very much for showing up in this thread, I appreciate a small answer too a lot :-)

Bye,
bearophile



More information about the Digitalmars-d mailing list