duk*_*kky 5 c++ memory cygwin g++ segmentation-fault
我用c ++编写了一个主筛程序,它使用~12GB ram来计算低于100,000,000,000(1000亿)的所有素数.
使用Visual Studio 2012(在为x64设置的项目中)以及64位linux上的g ++编译时,该程序工作正常.但是,当在Windows 7 Home Premium 64位上用cygwin64中的g ++编译时,尝试使用超过~2GB的ram(运行筛子> ~17,000,000,000)时会发生分段错误
我很确定它是作为64位进程运行的,因为任务管理器中的进程名称旁边没有*32.
代码:
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;
long long sieve(long long n);
int main(int argc, char** argv) {
const long long ONE_BILLION = 1000*1000*1000;
if(argc == 2)
cout << sieve(atol(argv[1])) << endl;
else
cout << sieve(ONE_BILLION * 100) << endl;
}
long long sieve(long long n) {
vector<bool> bools(n+1);
for(long long i = 0; i <=n; i++)
bools[i] = true;
double csqrtn = sqrt(n);
for (long long i = 2; i < csqrtn; ++i)
if (bools[i])
for (long long j = i * i; j < n; j += i)
bools[j] = false;
long long primes2 = 0;
for (long long i = 2; i < n; i++)
if (bools[i])
primes2++;
return primes2;
}
Run Code Online (Sandbox Code Playgroud)
在Visual Studio中工作正常:

在x64 linux上正常工作:

用命令编译:
$ g++ -O3 sieve.cpp -o sieve.exe
运行180亿失败:
$ ./sieve.exe 18000000000
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
工作正常(根据任务经理使用2,079,968 K内存,但我的声誉不允许我发布第三个链接.)
$ ./sieve.exe 17000000000
755305935
Run Code Online (Sandbox Code Playgroud)
g ++版本:
$ g++ --version
g++ (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
注意:如果您打算尝试自己运行,可能需要很长时间.在3570k @ 4.2GHz运行1000亿视觉工作室需要大约30分钟,10亿大约10秒.但是,您可以仅使用向量分配来复制错误.
编辑:因为我没有明确提出一个问题:为什么会发生这种情况?它是cygwin64 dll的限制(cygwin64仅在一个月前完全发布)?