R
robertwessel2
There is another case, where this will not work:
When there are many functions with a huge size of
automatic storage, but during each run of the program
only one of them is being called.
With memory allocation at run-time only the memory
for the one function called will be allocated, which
might still fit into available memory.
When automatic memory is allocated statically once for
every function, the total amount of memory allocated
might exceed memory limitations.
While that's true, and might be a problem in some cases, it doesn't
invalidate the scheme. You just have a QoI issue where the static
allocation of your program is larger than you'd hope for. Nor is it
necessarily a problem - if these functions with huge automatic
allocations are all leaf functions (or otherwise guaranteed not to be
in each other's call chains), it would be trivial for the compiler to
share a static area for the automatics for all of those functions.
And then there's the flip side - stacks are often much, much smaller
than the possible static area, and functions that have "huge"
automatic areas tend not to fit well on the stack, which will might
them fail in any case. On the third hand, some compilers will
allocate excessively large automatic areas on the heap, which solves
the problem in either case.