Variable scope: inside or outside the loop
Per Bothner
per@bothner.com
Mon Aug 30 17:32:00 GMT 2004
Reshat Sabiq wrote:
> Stylistically i like and usually declare variables inside the loop, when
> they are not used outside the loop, and no avoidable malloc overhead is
> caused.
> However, if the compiler is not smart enough to push and pop only once,
> outside the loop, in this case each iteration of the loop will run with
> the unnecessary overhead of push and pop.
Declarations do not cause pushes/pops. Moving a declaration (as opposed
to the initialization) does not change the generated bytecode. (It may
change the debugging info.)
> P.S. Does the answer change from Java, to C++, to VC++?
No.
> P.P.S. If there is no definitive answer, should the second approach be
> used just out of caution?
No - use the first approach.
--
--Per Bothner
per@bothner.com http://per.bothner.com/
More information about the Java
mailing list