Why auto variables NOT overlap on stack?

John Carter john.carter@tait.co.nz
Mon Feb 8 00:42:00 GMT 2010


On Thu, 4 Feb 2010, Ding Curie wrote:

> Hello, everyone!
>
> I'm with the latest MinGW, and following program:
>
> -----------------------------------------------------------------------
> char *p1, *p2;
>
> int main(void)
> {
>    {
>        char a[1024];
>        p1 = &a[0];
>    }
>
>    {
>        char a[1024];
>        p2 = &a[0];
>    }
>
>    printf("p1 - p2 = %d\n", p1 - p2);
>
>    return 0;
> }
> -----------------------------------------------------------------------
>
> output:
>> p1 - p2 = 1024
>
> (Optimize or not makes no difference.)
>
> That means the two arrays don't overlap on the stack. But since that
> will save a significant amount of stack space, I wonder, why gcc don't
> manage to make the two arrays share one same memory block of 1K bytes
> on the stack?

Try fiddle with these two options...


`-fno-defer-pop'
      Always pop the arguments to each function call as soon as that
      function returns.  For machines which must pop arguments after a
      function call, the compiler normally lets arguments accumulate on
      the stack for several function calls and pops them all at once.

      Disabled at levels `-O', `-O2', `-O3', `-Os'.
`-fconserve-stack'
      Attempt to minimize stack usage.  The compiler will attempt to use
      less stack space, even if that makes the program slower.  This
      option implies setting the `large-stack-frame' parameter to 100
      and the `large-stack-frame-growth' parameter to 400.


gcc tends to just let the stack grow and grow then shrink it in one go
on function exit rather than grow shrink grow shrink..

Faster that way, converts N stack grows and shrinks into one big one.


John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter@tait.co.nz
New Zealand



More information about the Gcc-help mailing list