From owner-freebsd-arch@FreeBSD.ORG Mon Jan 31 17:04:34 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 A42F416A4CE for ; Mon, 31 Jan 2005 17:04:34 +0000 (GMT) Received: from mx1.originative.co.uk (freebsd.gotadsl.co.uk [81.6.249.198]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2180343D58 for ; Mon, 31 Jan 2005 17:04:34 +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 2294515575; Mon, 31 Jan 2005 17:04:33 +0000 (GMT) Received: from mx1.originative.co.uk ([127.0.0.1])port 10024) with ESMTP id 38650-08; Mon, 31 Jan 2005 17:04:18 +0000 (GMT) Received: by mx1.originative.co.uk (Postfix, from userid 1000) id 0B0F815579; Mon, 31 Jan 2005 17:04:18 +0000 (GMT) Date: Mon, 31 Jan 2005 17:04:17 +0000 From: Paul Richards To: Ulrich Spoerlein Message-ID: <20050131170417.GW61409@myrddin.originative.co.uk> References: <20050131122609.GA83556@gurney.reilly.home> <90392.1107174969@critter.freebsd.dk> <20050131163117.GE828@galgenberg.net> <20050131165817.GV61409@myrddin.originative.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050131165817.GV61409@myrddin.originative.co.uk> User-Agent: Mutt/1.5.6i cc: arch@freebsd.org cc: Poul-Henning Kamp 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 17:04:34 -0000 On Mon, Jan 31, 2005 at 04:58:17PM +0000, Paul Richards wrote: > On Mon, Jan 31, 2005 at 05:31:17PM +0100, Ulrich Spoerlein wrote: > > On Mon, 31.01.2005 at 13:36:09 +0100, Poul-Henning Kamp wrote: > > > >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. > > > > > > I would _really_ hope we have the compiler warning about this > > > already ? > > > > Doesn't look so: > > #include > > #include > > > > int > > main(int argc, char **argv) { > > int N = 42; > > int i; > > for (int i = 0; i < N; i++) > > if (i == 23) > > break; > > printf("%d\n", i); /* use finishing index */ > > return (0); > > } > > > > % cc -Wall -std=c99 test.c && ./a.out > > 1 > > gcc should be throwing an uninitialised warning here. With the right warns it does :-) cc -Wall -std=c99 -O -Wuninitialized test.c test.c test.c: In function `main': test.c:7: warning: 'i' might be used uninitialized in this function -- Paul Richards