Fix g++ warning: variables created in a for loop should not be used outside of it.