[comp.lang.c] local variables

cef@H.GP.CS.CMU.EDU (Charles Fineman) (10/23/87)

My question relates to the way storage is allocated for
local varaibles (local to a block that is). For example,
how would space be allocated for an invocation of the 
following procedure:

fred()

{
  int v1;
  
  { int v2; }
    
}

The question is: "Do most C compilers allocate space for 
v2 as soon as fred is invoked, or do they wait until
control passes into the block containing v2 before
allocating space on the stack for it?"

The compilers from my experience all seem to allocated space
for *all* variables declared withing a procedure. Even those
in nested blocks. I am intrested in knowing wether I can
count on this behaviour in most real-world C compilers (or 
other languages for that matter). 

In case you want to know why:
  Part of my job is building a pre-processor for C that, among
 other things, does inline procedure calls. Since the main reason
 for doing things inline is to alleviate the overhead of
 stack allocation, any compiler not doing all-at-once allocation
 will blow away some of the efficiency of the inline calls.

  Of course, I can get around this by pulling all the local 
 declarations in the invoked procedure to the top level of the
 calling procedure, but this gets to be rather messy (in terms
 of coding and readability).


	Charles Fineman
	Carnegie-Mellon University
	cef@h.cs.cmu.edu (via seismo)