Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!zaphod.mps.ohio-state.edu!brutus.cs.uiuc.edu!apple!snorkelwacker!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: When should variables within blocks be allocated? Message-ID: <15699@haddock.ima.isc.com> Date: 19 Jan 90 20:42:09 GMT References: <253@usblues.UUCP> <26449@stealth.acf.nyu.edu> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Distribution: usa Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 19 In article <26449@stealth.acf.nyu.edu> brnstnd@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@haddock.isc.com or ima!haddock!karl), The Walking Lint