And so and so forth. Never heard about a for loop???
Your problems might be caused by memory corruption or a shortage of memory.
Shorttage of memory can be monitored by comparing the stack an heap pointer regularly in loop.
Memory corruption is often caused by writing outside array bounds. So, maybe you should check each memcopy and each writing to arrays.
How much memory is used according to the compiler? (This is shown in a message just after successful compilation).