编译C++代码会使系统挂起

yas*_*ith 5 c++ linux compilation g++

当我尝试通过发出命令编译此文件时,"g ++ qr.cpp -o qr"系统挂起.我没有在其他任何地方看到过这种错误.

#include<iostream>

using namespace std;

bool win[1000000001];
bool know[1000000001];

int sixes[] = {6, 36, 216, 1296, 7776, 46656, 279936, 1679616, 10077696, 60466176, 362797056};

bool check(int n){
   cout << n << endl;
   if(!know[n]){
      bool b = check(n-1);
      for(int i=0; i<11; i++){
         if(n > sixes[i]){
            b = b & check(n-sixes[i]);
         }
      }
      win[n] = !b;
   }
   return win[n];
}

int main(){
   win[1] = know[1] = true;
   for(int j=0; j<11; j++){
      win[sixes[j]] = know[sixes[j]] = true;
   }
   int n = 1; 
   cin >> n;
   int i = 0;
   while(n != 0){
      i++;
      win[n] = check(n);
      cout << i << (win[n]?"-Heckle":"-Jeckle");
      cin >> n;
      if(n!=0) cout << endl;
   }
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的编译器版本信息如下.

yasith@vostro:~/Dropbox/Shared$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 
Run Code Online (Sandbox Code Playgroud)

Mys*_*ial 13

你知道这些有多大吗?

bool win[1000000001];
bool know[1000000001];
Run Code Online (Sandbox Code Playgroud)

那些至少1GB!你想要动态分配它们......


sno*_*rpe 5

它在我的Debian系统上使用g ++ 4.6.1编译得很好,它只有1GB的内存.

我尝试在更改数组大小时查看编译器和链接器的各种传递所使用的内存,并且内存使用没有太大变化,表明编译器没有尝试分配任何与之成比例的数据结构数组大小.

但是,我安装了新的GNU链接器"gold".

然后我再次尝试使用旧的(基于"BFD的")GNU链接器,这仍然是许多系统的默认链接步骤 - 然后我的系统开始像疯了一样捶打(我不得不杀死链接器进程) !

所以看起来新的黄金链接器比大型链接器更聪明.

在Debian上,只需安装"binutils-gold"软件包就可以将gold安装为系统链接器.[我不知道Ubuntu是否有相同的包,但由于Ubuntu基于Debian,似乎很可能.]