Arrays are 0 indexed. This means while you count 5 Values (1,2,3,4,5) Arrays count with: 0,1,2,3,4 your for-loop asks for smaller-equal to Buffersize (<=) while it should be <. Your array runs in the current state out of bounds which causes an overflow which again causes Undefined Behaviour