You can also add this line just before the loop:
ulimit -c unlimited
It will enable core dumps, so when your application crashes, core dump file will be created, which can be later opened in gdb to find information about the reason of the crash.
To avoid overriding core dump file in the...