c99/c++ localised variable definition

Ben Mesander ben at timing.com
Fri Jan 28 11:58:38 PST 2005


Peter Jeremy writes:
 > It is most useful for variables with a short lexical lifetime in large
 > functions.  For a variable with a long lifetime - especially one which
 > is infrequently referenced - it can make it much harder to locate the
 > variable definition.  It is also far less obvious what variable are
 > in scope at any point - which is an issue if you are writing some new
 > code and need a work variable.

On the positive side, it is also very useful for allowing you to make
things that shouldn't change really const.

You can declare:

   const int foo = bar*GRONK + 37;

in the middle of a block, and get a compile-time error if you try to
change it later while maintaining the code.

If you're forced to do all decl's at the top of a function, it's
somewhat more work to be const correct.

The other points you bring up are mostly big issues only if the
functions in question are quite large, which some (but I'm not sure
all) developers feel is indicative of other problems with the code.

Regards,
Ben


More information about the freebsd-arch mailing list