1

I get a segmentation fault when I initialise c inside main. But works fine when I initialise it globally.

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int c[2001][2001];

int main()
{ 
  // I get a segmentation fault when I initialise c inside main
  // But works fine when I initialise it globally.
  // int c[2001][2001];
  int T,n,m,i,j;
  for(i=0;i<2001;++i)
  {
    c[i][0]=1;
    for(j=1;j<=i;++j)
      c[i][j]=(c[i-1][j-1]+c[i-1][j])%MOD;
  }

  scanf("%d",&T);
  while(T--)
  {
    scanf("%d%d",&n,&m);
    printf("%d\n",c[n][m]);
  }
}
Backalla
  • 86
  • 8
  • 6
    You get a stackoverflow because the array is too big to fit on the stack. You should use `std::vector c(2001 * 2001);` – Borgleader Jan 30 '15 at 19:02
  • The global array is inside the memory the program (binary) occupies, the local array is using the limited stack space. –  Jan 30 '15 at 19:04
  • 1
    Four million `int` values requires about 16 MiB of stack. Most systems limit the stack to 8 MiB or less. There are a number of other questions on the same topic. – Jonathan Leffler Jan 30 '15 at 19:04

0 Answers0