Setting a large number of array values causes memory corruption