Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!purdue!tut.cis.ohio-state.edu!zaphod.mps.ohio-state.edu!samsung!xylogics!merk!alliant!linus!think!barmar From: barmar@think.com (Barry Margolin) Newsgroups: comp.lang.c++ Subject: Re: C++ coding standards (Comment needed) Keywords: standard,variables Message-ID: <41673@think.Think.COM> Date: 19 Aug 90 16:57:08 GMT References: <2161@runxtsa.runx.oz.au> <1990Aug13.182226.24141@alias.uucp> <56642@microsoft.UUCP> Sender: news@Think.COM Organization: Thinking Machines Corporation, Cambridge MA, USA Lines: 22 In article <56642@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: >In article <1990Aug13.182226.24141@alias.uucp> bmcphers@alias.UUCP (Brent McPherson) writes: >>Finally, common sense should prevail. If a variable is first assigned a value >>within a loop it should be declared outside the loop just in case the compiler >>decides to create the variable on the stack for each loop iteration. >It is certainly not the case that a thousand foos reside simultaneously >on the stack. Rather, it is guaranteed that an object is destroyed as >its constructor is jumped back over. Thus, while conceptually there >are a 1000 foos, only one is in existance at any point in time, and they >all reside at the same location on the stack. However, the compiler might decide to grow and shrink the stack frame each time the variable is created and destroyed. Also, if the constructor and/or destructor do anything expensive (e.g. use "new" and "delete" on members of the object) this will be done each time through the loop, when it probably would be OK to reuse a single object. -- Barry Margolin, Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar