Re: c99/c++ localised variable definition

From: Paul Richards (paul_at_originative.co.uk)
Date: 01/31/05

  • Next message: Ryan Sommers: "Re: c99/c++ localised variable definition"
    Date: Mon, 31 Jan 2005 12:57:43 +0000
    To: Andrew Reilly <andrew-freebsd@areilly.bpc-users.org>
    
    

    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
    _______________________________________________
    freebsd-arch@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-arch
    To unsubscribe, send any mail to "freebsd-arch-unsubscribe@freebsd.org"
    

  • Next message: Ryan Sommers: "Re: c99/c++ localised variable definition"

    Relevant Pages

    • Re: About String
      ... My idea is that every statement list is a scope. ... You declare something inside a loop body, ... The same sense as declare blocks. ... SCOPE [DECLS] STATEMENTS END-SCOPE ...
      (comp.lang.ada)
    • Re: Which scope for variables being used in a loop?
      ... can't be GC'd until the scope they are declared in exist. ... inside of the loop. ... overlay local allocations in another loop. ... declaration itself, such that the pointer-value of the variable is ...
      (comp.lang.java.programmer)
    • Re: Scope Best Practice
      ... creating a new object versus the cost of maintaining it in memory?" ... the object is ready for GC once the loop is complete ( ... even if it remains in scope because it is not being referenced anywhere ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: NAG, Sun, Compaq possible f95 bug
      ... Range of a DO loop and scope are different. ... that wasn't mean to say that Lahey was nonconforming ... and thus the compiler is allowed to ...
      (comp.lang.fortran)
    • Re: Dereferencing Hash of Arrays
      ... I guess it has the opposite effect. ... is to always declare variables in the shortest scope possible. ... Change that while loop to: ... iteration begins, %data will be destroyed and your next iteration will ...
      (comp.lang.perl.misc)