Thomas Thomas - 3 months ago 17
C++ Question

My prime number generator crashes and is closed by Windows 10

I have used codeblocks in the past also but never I faced such a problem.

I wrote a prime number generator between numbers 1 to 1000 and when I click on build > build and run, I get a dialog, "A problem caused the program to stop working correctly..." and I have only one option, that is to close the program.

After I close the program, what I see on the console window is "Process returned 255 (0xFF)". When I close the console window, I see white text highlighted with red saying "process terminated with status -1073741510".

The same program used to run perfectly before I had resetted my pc. It is running on Windows 10.

Any help will be appreciated.

My code is -

#include <iostream>
using namespace std;
int main() {
int facts = 0;
for(int i = 0; i < 1001; i++) {
facts = 0;
for(int j = 0; j < (i + 1); i++) {
if(i % j == 0) {
facts ;
}
}
if(facts > 2) {
cout<<i;
}
}
return 0;
}

Answer

Have you tried using a debugger? I took your code, added the missing ++ in front of facts in the inner loop, and look:

$ g++ -std=c++14 -Wall -Wextra -Wpedantic -ggdb test.cpp
$ gdb ./a.out
+(gdb) run

Starting program: /home/???/src/a.out 

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400702 in main () at test.cpp:8
8               if(i % j == 0) {
+(gdb)

If I add a diagnostic cout << i << ' ' << j << std::endl just before that line, all that is ever printed is this...

0 0

...right before gdb fails out with the SIGFPE. So, the loop fails on the 1st try. Why? Because modulo is the other side of division, the quoted line attempts to divide by zero. You can't divide by zero. Do not try to divide by zero.

Why? Well - as well as causing endless debates among (armchair?) mathematicians, - in C and C++, dividing by zero is one of many oopses that invoke undefined behaviour. You need to learn this concept, early and well. The compiler is not required to stop you doing such things. But the act renders your whole program invalid and prone to explode. My CPU raises a hardware exception. Yours might do the same, might do something else... UB means the program can do anything - including seemingly working fine till it mysteriously fails in an unrelated area and leaves you chasing red herrings for hours until you find the embarrassing cause. Trust me. That's why we don't invoke UB.

There are probably other errors in your code, but this is the most obvious - and easy to find - one. And as drescherjm pointed out...

for(int j = 0; j < (i + 1); i++) should be for(int j = 2; j < (i + 1); j++) then change (facts > 2) to (facts > 1)

...the attempted divide-by-0 is happening because your numbers are wrong.

The same program used to run perfectly before I had resetted my pc. It is running on Windows 10.

Again: undefined behaviour means anything can happen. It might start seeming to work again tomorrow or when the wind direction changes just right. That doesn't mean anything. Code invoking UB is ill-formed and must be avoided at all costs.