c99/c++ localised variable definition

Poul-Henning Kamp phk at phk.freebsd.dk
Mon Jan 31 04:36:19 PST 2005


In message <20050131122609.GA83556 at gurney.reilly.home>, Andrew Reilly writes:

>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 ?

-- 
Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG         | TCP/IP since RFC 956
FreeBSD committer       | BSD since 4.3-tahoe    
Never attribute to malice what can adequately be explained by incompetence.


More information about the freebsd-arch mailing list