fixes cppcheck errors

This PR fixes some cppcheck errors and uses pre-increment instead of post-increment in some for loops.