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..
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.