I've gone through the following code multiple times to figure out why I was getting a segfault on every run, but nothing worked. I tried making the "arr" declaration global, which didn't change anything. I also put a cout statement after the declaration to see if something else was causing the fault, but it didn't print anything, so it must be "arr" that's causing the trouble. I even tried just using 100 000 ints, but that didn't help either. I'm on Ubuntu 11.10, and it's for Project Euler problem 14 if it matters. Thanks for any help.
I know I'm initializing int i to 1, but I make sure to subtract 1 every time I use it to access an element in the array. Thanks though!
EDIT: @Peter87 Well I did do the "sanity check" right after initializing "arr", and I never got anything printed, so the error must be with the array, I just don't know why, and what to do.
Wow, thanks so much guys! I just ran it in MVC++ and I was indeed getting integer overflow! Changed temp to long long and everything worked perfectly! What threw me off was that my check didn't print! Thanks again!
the compiler probably optimized away the whole if statement. Signed overflow is undefined and there is no other way temp could get negative so the compiler is free to assume the code in the if statement will never run.