I have written a very basic sieve of eratosthenes with C++, however when the n is 1000000 (a million), the code crashes. I could not resolve the issue and now in need of help.
#include <iostream>
#include <vector>
using namespace std;
int main(){
long n = 1000000,i ,j;
vector<bool> arr(n, true);
for(i = 2; i * i < n; i++){
if(arr[i]){
for(j = i + i; j <= n; j += i){
arr[j] = false;
}
}
}
cout << "Made it here." << endl;
return 0;
}
Some info:
- I compile with x64 mingw (Windows 10) compiler.
- Using -O2, disabling makes no difference
n = 100000orn = 10000000does not give errors (very strange) and code works fine, so values more than a million does not give problems while one million gives an error (crash).- Code can print
"Made it here." - Tried making the vector global (thinking about local memory limit problem), no avail.
- Used DrMemory for debugging, it showed one error
1 total unaddressable access(es), which I could not find.
Thanks for your help.
Edit: A more informing description of the error from Drmemory
Error #1: UNADDRESSABLE ACCESS beyond heap bounds: writing 0x0000000003921608-0x000000000392160c 4 byte(s)
Aucun commentaire:
Enregistrer un commentaire