From owner-freebsd-arch@FreeBSD.ORG Mon Jan 31 16:31:27 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 AA35916A4CE for ; Mon, 31 Jan 2005 16:31:27 +0000 (GMT) Received: from wrzx28.rz.uni-wuerzburg.de (wrzx28.rz.uni-wuerzburg.de [132.187.3.28]) by mx1.FreeBSD.org (Postfix) with ESMTP id 36ABC43D39 for ; Mon, 31 Jan 2005 16:31:25 +0000 (GMT) (envelope-from q@uni.de) Received: from wrzx34.rz.uni-wuerzburg.de (wrzx34.rz.uni-wuerzburg.de [132.187.3.34]) by wrzx28.rz.uni-wuerzburg.de (Postfix) with ESMTP id D6393D849A; Mon, 31 Jan 2005 17:31:24 +0100 (CET) Received: from virusscan (localhost [127.0.0.1]) by wrzx34.rz.uni-wuerzburg.de (Postfix) with ESMTP id B6A15B178B; Mon, 31 Jan 2005 17:31:24 +0100 (CET) Received: from wrzx28.rz.uni-wuerzburg.de (wrzx28.rz.uni-wuerzburg.de [132.187.3.28]) by wrzx34.rz.uni-wuerzburg.de (Postfix) with ESMTP id 9AFE8B1726; Mon, 31 Jan 2005 17:31:24 +0100 (CET) Received: from coyote.q.local (wwsx14.win-screen.uni-wuerzburg.de [132.187.253.14]) by wrzx28.rz.uni-wuerzburg.de (Postfix) with ESMTP id F32C1D849A; Mon, 31 Jan 2005 17:31:23 +0100 (CET) Received: from roadrunner.q.local (roadrunner.q.local [192.168.0.148]) by coyote.q.local (8.13.1/8.13.1) with ESMTP id j0VGVNE9070149; Mon, 31 Jan 2005 17:31:23 +0100 (CET) (envelope-from q@uni.de) Received: from roadrunner.q.local (localhost [127.0.0.1]) by roadrunner.q.local (8.13.1/8.13.1) with ESMTP id j0VGVNlf005775; Mon, 31 Jan 2005 17:31:23 +0100 (CET) (envelope-from q@uni.de) Received: (from q@localhost) by roadrunner.q.local (8.13.1/8.13.1/Submit) id j0VGVH9W005774; Mon, 31 Jan 2005 17:31:17 +0100 (CET) (envelope-from q@uni.de) Date: Mon, 31 Jan 2005 17:31:17 +0100 From: Ulrich Spoerlein To: Poul-Henning Kamp Message-ID: <20050131163117.GE828@galgenberg.net> References: <20050131122609.GA83556@gurney.reilly.home> <90392.1107174969@critter.freebsd.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <90392.1107174969@critter.freebsd.dk> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new (Rechenzentrum Universitaet Wuerzburg) cc: Paul Richards 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:31:27 -0000 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 % 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. Ulrich Spoerlein -- PGP Key ID: F0DB9F44 Encrypted mail welcome! Fingerprint: F1CE D062 0CA9 ADE3 349B 2FE8 980A C6B5 F0DB 9F44 Ok, which part of "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn." didn't you understand?