Please change the condition to pi->cur < pi->end. Safer in case there is a double increment.