Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!microsoft!jimad From: jimad@microsoft.UUCP (Jim ADCOCK) Newsgroups: comp.lang.c++ Subject: Re: C++ coding standards (Comment needed) Keywords: standard,variables Message-ID: <56953@microsoft.UUCP> Date: 27 Aug 90 19:04:46 GMT References: <2161@runxtsa.runx.oz.au> <1990Aug13.182226.24141@alias.uucp> <56642@microsoft.UUCP> <41673@think.Think.COM> Reply-To: jimad@microsoft.UUCP (Jim ADCOCK) Organization: Microsoft Corp., Redmond WA Lines: 24 In article <41673@think.Think.COM> barmar@think.com (Barry Margolin) writes: >In article <56642@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes: >>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. Hm, do you know of any C++ compiler that actually grows and shrinks a the stack within a routine? [in the absense if alloca()s] A while back I read a survey of C compilers that could identify no such compiler. It turns out gotos in the language makes doing such problamatic. Bottom line in any case: if an object's construction and assignment work more or less identically, you can just put the constructor outside the loop, and assign inside the loop. If an object's constructor and assignment work considerably differently, you might need to move the constructor inside the loop.