Valid indexes for foo
are from 0
to MAX-1
inclusive. MAX
is past the end of the array.
Your loop runs up to, and including, MAX
. This writes beyond the end of the array, corrupting the stack.
Either increase the array size to MAX+1
so that MAX
is in range; or change the loop condition to i < MAX
to stop before reaching MAX
.