in reply to how to debug a segmentation fault

What are you actually doing in the loop? If you are modifying the number of element of the array on which you are looping, you might encounter some problems (although I can't remember if this type of things creates a segmentation fault or some other problems).