When should variables within blocks be allocated?

Karl Heuer karl at haddock.ima.isc.com
Sat Jan 20 07:42:09 AEST 1990


In article <26449 at stealth.acf.nyu.edu> brnstnd at stealth.acf.nyu.edu (Dan Bernstein) writes:
>If a function contains the statement if(0) { int k[100000000]; ... },
>should the space for k be allocated?  Every compiler I've tried allocates
>variables at function entry like Tom's, but it would be nice to save
>100M of memory if k is never used. I can't find any ANSI rules on this.

The critical rule here is known as the "as-if" rule: the Standard does not
(indeed, cannot) say anything about what sort of "machine code" is generated;
it describes the behavior of conforming programs.  The implementation must
behave *as if* certain things were true, but if there is no way for a correct
program to detect the difference, then it is free to optimize things away.  In
particular, unused variables need not be allocated.  Such compilers exist.

>Are there any compilers that go all the way and allocate k the moment they
>enter the block?  Is the space saved worth the efficiency loss?

I've seen compilers that do this, but I generally consider it a loss.

Karl W. Z. Heuer (karl at haddock.isc.com or ima!haddock!karl), The Walking Lint



More information about the Comp.lang.c mailing list