Actually I am deleting atoms on the fly within the loop I mentioned and may be in the next iteration the atom index is being referenced to the atom that is deleted and that may be causing a segmentation fault.
Nevertheless I figured out another way to make the loop work
Thanks Dr. Axel