Originally Posted by poroboszcz
is too big to fit on the stack. Try using std::vector instead of C array. You also don't need to iterate all the way to theLimit
just up to (i * i < theLimit)
It is indeed too big to fit in the stack using the normal C array but it's not the only reason,this is the main one:
for (int factor = 2; currNum <= theLimit; ++factor)
instead it should be something like this:
for (int factor = 2; (currNum * factor) <= theLimit; ++factor)
Otherwise when currNum becomes big and then is multiplied by the factor it can produce a number bigger than theLimit,so when it executes this line
and then executes the line after
nonOddPrimes[[B]currNum[/B]] = true;
it tries to access an index that is beyond the range of the array and thus a runtime error.
The reason it took me so long to debug is because in visual C++ which I'm used to,when you try to access and index beyond the array's limit it gives me a stack overflow (That's the name of the error,right?) error with some memory adress and then I know that I'm trying to access an index that's not there,in G++ however,I get that weird segmentation fault error which I'm not fimiliar with.
You also don't need to iterate all the way to theLimit just up to (i * i < theLimit).
This is the biggest prime it found in the edited code:997.
This is the biggest prime it found in the original code:999983.