I suppose your calculation of it was wrong and gave positions bigger thant the size of the array for some values of nx/ny/nz ,or wrong positions when it was not overflowing.
Take a size of 10 in each dimension for example.
The last it calculated (for nx=ny=nz=9) would be
9+9*(9+9*9) = 9+9*90 = 819, when i think you would want 999