From owner-freebsd-arch@FreeBSD.ORG Mon Jan 31 12:58:03 2005 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 81AE416A4CE for ; Mon, 31 Jan 2005 12:58:03 +0000 (GMT) Received: from mx1.originative.co.uk (freebsd.gotadsl.co.uk [81.6.249.198]) by mx1.FreeBSD.org (Postfix) with ESMTP id CBA7A43D3F for ; Mon, 31 Jan 2005 12:58:00 +0000 (GMT) (envelope-from paul@mx1.originative.co.uk) Received: from localhost (unknown [127.0.0.1]) by mx1.originative.co.uk (Postfix) with ESMTP id B575115575; Mon, 31 Jan 2005 12:57:59 +0000 (GMT) Received: from mx1.originative.co.uk ([127.0.0.1])port 10024) with ESMTP id 50905-06; Mon, 31 Jan 2005 12:57:43 +0000 (GMT) Received: by mx1.originative.co.uk (Postfix, from userid 1000) id A879615579; Mon, 31 Jan 2005 12:57:43 +0000 (GMT) Date: Mon, 31 Jan 2005 12:57:43 +0000 From: Paul Richards To: Andrew Reilly Message-ID: <20050131125743.GO61409@myrddin.originative.co.uk> References: <20050128173327.GI61409@myrddin.originative.co.uk> <20050131102630.GJ61409@myrddin.originative.co.uk> <20050131122609.GA83556@gurney.reilly.home> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050131122609.GA83556@gurney.reilly.home> User-Agent: Mutt/1.5.6i cc: arch@freebsd.org Subject: Re: c99/c++ localised variable definition X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 31 Jan 2005 12:58:03 -0000 On Mon, Jan 31, 2005 at 11:26:09PM +1100, Andrew Reilly wrote: > On Mon, Jan 31, 2005 at 10:26:30AM +0000, Paul Richards wrote: > > 3) Usage in for loops may be more useful than other uses. > [snip] > > I think the loop usage though is one clear example where it is > > clearer. I think there are others as well; where the usage of the > > variable is clearly localised it is much easier to see a local > > definition than to have to jump back and forth to find out what > > variables are. > > I'd just like to raise a dissenting voice to this particular > point. I find the for-loop initialization syntax a pernicious > source of errors, mainly because of the non-intuitive scope of > the definition. I.e., it looks like it's equivalent to "int i; > for (i = 0;;)" but it isn't. > > If you carelessly c++-ify a loop like: > > for (int i = 0; i < N; i++) > { > if (some_condition(i)) break; > } > do_something_with(i); /* use finishing index */ > > you can miss the fact that the value of i is used outside of the > loop. The newly created scope for "i" shadows the presumably > pre-existing definition of i at the top of the function, which > is what do_something_with() gets to see. Depends whether you find it non-intuitive or not. I find this usage makes it clearer that the loop variable only exists within the scope of the loop. If I needed to code the above example then I know that my for construct is doing more than just looping, it's also returning a value and therefore it needs to affect a variable outside of its scope. I think use of this syntax makes it clearer what you intend in each case. The converse is true as well though, where a global is inadvertently used as a loop variable. -- Paul Richards