From owner-freebsd-arch@FreeBSD.ORG Mon Jan 31 16:58:33 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 DAB9A16A4CF for ; Mon, 31 Jan 2005 16:58:33 +0000 (GMT) Received: from mx1.originative.co.uk (freebsd.gotadsl.co.uk [81.6.249.198]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5598B43D1D for ; Mon, 31 Jan 2005 16:58:33 +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 44D0915575; Mon, 31 Jan 2005 16:58:32 +0000 (GMT) Received: from mx1.originative.co.uk ([127.0.0.1])port 10024) with ESMTP id 76384-03; Mon, 31 Jan 2005 16:58:17 +0000 (GMT) Received: by mx1.originative.co.uk (Postfix, from userid 1000) id 63D5D15579; Mon, 31 Jan 2005 16:58:17 +0000 (GMT) Date: Mon, 31 Jan 2005 16:58:17 +0000 From: Paul Richards To: Ulrich Spoerlein Message-ID: <20050131165817.GV61409@myrddin.originative.co.uk> References: <20050131122609.GA83556@gurney.reilly.home> <90392.1107174969@critter.freebsd.dk> <20050131163117.GE828@galgenberg.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050131163117.GE828@galgenberg.net> User-Agent: Mutt/1.5.6i cc: Poul-Henning Kamp 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 16:58:34 -0000 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. > % icc -Wall -std=c99 test.c && ./a.out > test.c(12): remark #592: variable "i" is used before its value is set > printf("%d\n", i); /* use finishing index */ > ^ > 0 > > But the ICC warning is bogus too, if you happen to set i before, the > warning disappears. icc looks correct to me since the i being printed out is the one in the outer scope which is unitialised. -- Paul Richards