send_nodes's blog

By send_nodes, history, 9 years ago, In English

I've been having a problem with 2-D arrays for a while. If I allocate a 2-D array with dimensions 1000x1000, then when I debug, the program crashes. For example, this code is crashing:

#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

//macros
typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector< vector<int> > vvi;




#define FOR(k,a,b) for(int k=(a); k<=(b); ++k)
#define REP(k,a) for(int k=0; k<(a);++k)
#define SZ(a) int((a).size())
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define INF 99999999
#define MOD 1000000007
#define MAX 100000
#define ITERS 10000
#define pi 3.1415926

int main(){
	int n,m;
	cin >> n >> m;
	int arr[1000][1000];
	REP(i,n){
		REP(j,m){
			int nxt;
			cin >> nxt;
			arr[i][j] = nxt;
		}
	}
	int dpa[1000][1000];
	int dpb[1000][1000];
}

I'm using MinGW GCC g++ 4.8.1. Any ideas on what the issue is? When I run, I get an error message saying:

".exe has stopped working. Windows is checking for a solution to the problem..."

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 years ago, # |
Rev. 2   Vote: I like it +17 Vote: I do not like it
  • »
    »
    9 years ago, # ^ |
      Vote: I like it -11 Vote: I do not like it

    That didn't fix it.

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it +19 Vote: I do not like it
      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it +9 Vote: I do not like it

        But the problem is specific to my compiler. If I compile on ideone, it will run just fine. If it's on my computer, it won't run. It's probably a stack size problem.

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      It should help. Would you mind trying running the exact code from darkprinx's comment above?

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        My apologies, it worked this time. I must have had a permission denied error in eclipse when I ran it a few hours ago. I guess I better get used to declaring variables globally :D

        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it +1 Vote: I do not like it

          If you still want to make arrays locally, you can do it with malloc or use vectors, so that the memory comes from the heap rather than going on the stack.

          This can come in handy in situations where you don't want to deal with global variables, e.g. problems with multiple test cases in one run.

          • »
            »
            »
            »
            »
            »
            9 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            One shouldn't use malloc when vectors are available without a very-very good reason.

»
9 years ago, # |
  Vote: I like it +4 Vote: I do not like it

You need to set stack size to 256MB. Check this link.

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I think this is the fix, but how can I enter the command into GCC? Through command prompt? Will that permanently set the stack size?