Certain input value causing crash

Dear Users,

The code blow keeps craching for input 15. I thought it was some Windows 7 issue. Then I installed a fresh copy of Windows 10. But still the exe keeps craching for input 15. Any ideas? Best regards..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <chrono>
#include <iomanip>
using namespace std;

int main(int argc, char** argv) {
INPUT:int k;
cout << "Input nth prime: ";
cin >> k;
int l, m=3, n, o;
int*p=new int[k-1]; p[0]=2, p[1]=m;
auto start_time = std::chrono::system_clock::now();

for(n=2;m+=2,n<k;p[n]=m,n++,l=1)
for(o=3;o*o<=m;(m%o==0?m+=2,l=1:l++),o=p[l]);

std::chrono::duration<double> elapsed_time = std::chrono::system_clock::now() - start_time;
cout << "Time elased " << std::fixed << setprecision(3) << elapsed_time.count() << " seconds" << std::endl;
cout << std::to_string(k) << ". prime is: " << std::to_string(p[k-1]) << std::endl << std::endl;
delete[] p;
goto INPUT;
}
Last edited on
1
2
  for (n = 2; m += 2, n < k; p[n] = m, n++, l = 1)
    for (o = 3; o * o <= m; (m % o == 0 ? m += 2, l = 1 : l++), o = p[l]);

The first step would be to replace this ridiculously compressed code with something that is actually readable.

It's obviously got a bug, but you're completely screwed in terms of being able to analyse and fix the problem.
I like compressed code as much as anyone, but it's an absolute nightmare to debug if it doesn't work

Unwind the for loops into 'simple' code that can be traced and use the debugger to find out where is the problem. Once you get it working with code that can be traced/debugged, 'wind' the code back into the for loops if you really must - but why??


$ valgrind ./a.out
Input nth prime: ==8120== Invalid write of size 4
==8120==    at 0x134D11: main (foo.cpp:15)
==8120==  Address 0x4dc0598 is 0 bytes after a block of size 56 alloc'd
==8120==    at 0x483B50F: operator new[](unsigned long) (vg_replace_malloc.c:431)
==8120==    by 0x13480C: main (foo.cpp:12)
==8120==
Time elased 0.012 seconds
==8120== Invalid read of size 4
==8120==    at 0x135356: main (foo.cpp:20)
==8120==  Address 0x4dc0598 is 0 bytes after a block of size 56 alloc'd
==8120==    at 0x483B50F: operator new[](unsigned long) (vg_replace_malloc.c:431)
==8120==    by 0x13480C: main (foo.cpp:12)
==8120==
out of bound access

also, you may print a number, no need to convert it to string first
The shell game with k is probably the issue.
new memory [k-1]
n < k
...musical chairs
... out of bounds using something derived from k to access something sized k-1 and reaching k in the loops...
rewrite lines 15 and 16 cleanly and it should correct itself.

p[n] = m, n is < k but p allocated k-1 looks mighty suspect. I am not sure wtf happens to L .. I could probably figure it out, but there isnt any point, just rewrite it correctly.
Last edited on
Change to

 
	int* p = new int[k]; p[0] = 2, p[1] = m;


works ok on my windows computer with n 900000 (13834103)

Rather than int, you might want to use unsigned long long to get a better upper value ( 18,446,744,073,709,551,615)
Topic archived. No new replies allowed.