This is writing one byte after the array. I think you want [511]. Same below.