[comp.lang.c] Local Variable Storage Scope

unhd (Anthony Lapadula) (07/24/90)

I've found some interesting behaviour in our current C compiler (gcc 1.36).

   void f()
      {
           {  int local1[100]; /* some code goes here */  }
           {  int local2[100]; /* more code goes here */  }
      }


Is the compiler allowed to allocate just enough space for only
one of the local arrays?  That is, can ``local1'' and ``local2''
share the same spot on the run-time stack?

Gcc does *not* do this, but instead reserves enough room on the
stack for both arrays.  I couldn't come up with an explanation
for this behaviour.  Is there one?

BTW, the code in question is being generated by another program.
*Lots* of local arrays are declared, but the code must run on a
machine with 512K RAM and no VM -- so every bit of stack space helps.

-- Anthony (uunet!unhd!al) Lapadula

henry@zoo.toronto.edu (Henry Spencer) (07/24/90)

In article <1990Jul23.230418.15169@uunet!unhd> al@uunet!unhd (Anthony Lapadula) writes:
>           {  int local1[100]; /* some code goes here */  }
>           {  int local2[100]; /* more code goes here */  }
>
>Is the compiler allowed to allocate just enough space for only
>one of the local arrays?  That is, can ``local1'' and ``local2''
>share the same spot on the run-time stack?

Certainly.  local1 exists only inside the first block, local2 exists only
inside the second, so it is perfectly legitimate to re-use the storage that
used to hold local1 for local2.  Mind you, there is no promise that this
will be done.
-- 
NFS:  all the nice semantics of MSDOS, | Henry Spencer at U of Toronto Zoology
and its performance and security too.  |  henry@zoo.toronto.edu   utzoo!henry