Frame pointer, bug or feature? (x86)

Jakub Jelinek jakub@redhat.com
Mon Nov 18 20:55:00 GMT 2013


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



More information about the Gcc mailing list