Fix for loops and eliminate some other undesirable idioms.

Previously, we were using a non-standard C dialect which did not permit
the nicer variable scoping in loops.  Now that we are using C++ in the
collector we can guarantee that feature is available.  This change also
rearranges some of the surrounding code to take advantage of the more
flexible scoping rules.

Change-Id: I9be35794cc12bcbc0d5299fe387d4bc406481075
9 files changed