My guess is that you are correct: due to the infinite loop, your program has undefined behaviour when the array is written to out of bounds. A simple fix is to change that while loop to a for loop...