This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Why auto variables NOT overlap on stack?


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?

BTW, I checked two embedded C compilers: CCS from TI and VisualDSP
from ADI, do the trick, outputing p1 - p2 = 0.

Thanks in advance.

Ding Curie


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]