I see what you're saying. You mean because the VLA stack space can be
dynamically "free'd" right away, as opposed to be there until the
epilogue. That is true :( Is still seems odd when just looking at it.
It's hard to imagine somebody would actually code
myarray[const_thousand_var] as opposed to myarray[1000] with the
intention to control stack allocation... thanks though

On Mon, Nov 18, 2013 at 12:54 PM, Jakub Jelinek <ja...@redhat.com> wrote:
> On Mon, Nov 18, 2013 at 12:43:50PM -0800, Hendrik Greving wrote:
>> Hmm don't VLA's obey the same lifetime rules as regular automatic
>> arrays on the stack?
>
> In the languages yes, in GCC no.  There is code to determine possibilities
> of sharing some stack space between variables that can't be used at the same
> time, but all the stack space for addressable automatic variables is
> typically allocated in function prologue and deallocated in the epilogue.
>
> So, if you have say:
> extern void baz (char *);
>
> __attribute__((noinline)) void
> bar (void)
> {
>   char buf[7 * 1024 * 1024];
>   baz (buf);
> }
>
> void
> foo (void)
> {
>   bar ();
>   {
>     const int length = 5 * 1024 * 1024;
>     char buf[length];
>     baz (buf);
>   }
>   bar ();
> }
>
> and say typical Linux stack limit of 8-10MB, then if baz function (nor
> anything it calls) doesn't need much stack space, nor foo callers, then
> if buf[length] is a VLA, it will probably work just fine, if GCC decided
> to optimize it into char buf[5 * 1024 * 1024]; instead, it would likely
> fail.
>
>         Jakub

Reply via email to