Using `const int' values to size stack arrays

Robert Dewar dewar@gnat.com
Wed Nov 17 13:53:00 GMT 2004


Nathan Sidwell wrote:

> sorry, I missed this point of your email.  I don't think we can arbitrarily
> remove the dynamic allocation of a VLA, because I think the following
> is required to work
> 
> static const int SIZE = SOME_HUGE_VALUE_THAT_CAUSES_STACK_OVERFLOW;
> 
> void Foo ()
> {
>   if (SOME_EXPR_THAT_IS_FALSE_AT_RUNTIME)
>     {
>     char ary[SIZE];
>     }
> }
> 
> If ary was promoted to a regular array, and therefore stack-frame
> allocated at compile time, this function would fail when called, rather
> than fail when the if block is entered.  (A pathelogical example)

OK, so if this pathology is worrying you, limit the optimization to cases
where the array is "small" (after all these are cases where the compiler
does in fact know the size :-)



More information about the Gcc mailing list