This is the mail archive of the java@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Variable scope: inside or outside the loop


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.
Example:
while(i.hasNext()) {
	MyRef mr = (MyRef)i.next();
	// do something with mr
}
Should this be changed to:
MyRef mr;
while(i.hasNext()) {
	mr = (MyRef)i.next();
	// do something with mr
}

P.S. Does the answer change from Java, to C++, to VC++?
P.P.S. If there is no definitive answer, should the second approach be
used just out of caution?

Thanks,
<rsa/>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]