i'm using gcc for a group theory project, and i feel quite bad now because my program always crashes with bigger data boundarys (maxn), and i have no idea why since about 2 days

.
my data is a simple array [100] of permutations (a simple class which itself contains an array of size maxn=1000 of integers)
i'm using new [] to allocate the memory, and i delete[] it at the end of the program.
the software works great for maxn=1000, but it crashes at positions that do not make any sense if i set maxn=1500 or something bigger

.
SIGSEGV, or gdb says EXC_BAD_ACCESS
what am i doing wrong with my memory ? i mean it should be possible to have 100*1500 int's if i allocate the memory ? i would really need 5000*5000 int's in future...
to test the programm paste the following input (whitespace does not matter) after start (it is a simple command line program):
100
2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
100 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
the code is the following (i deleted everything not needed to make it as simple as possible to reproduce the error)
Code:
#include <iostream.h>
using namespace std;
const int maxn = 1500; // Maximum size of omega = {1, ,n}
class Permutation { // interface for permutations
public:
int p[maxn]; // the images of the points 0..maxn-1
int n; // size of omega = {1, ,n}
Permutation () { n=maxn; }; // constructors
Permutation (int m, char c) { n=m; switch(c) {
case 'i': for (int i=0;i<n;i++) p[i] = i; break; // identity
default: for (int i=0;i<n;i++) p[i] =-1; break; // undefined
} };
void operator *= (Permutation param) { // direct multiplication
for (int i=0;i<n;i++) p[i] = param.p[ p[i] ];
};
Permutation inverse() const { // inverse
Permutation result (n,'i');
for (int i=0;i<n;i++) result.p[ p[i] ] = i;
return (result);
};
bool isdefined() const { return (p[0] > -1); }; // if it is defined
void input() { for (int i=0;i<n;i++) { cin >> p[i]; p[i]--; } }; // input
void output() const { for (int i=0;i<n;i++) cout << p[i]+1 << " "; cout << endl; }; // output
void setn(int m) { n=m; };
};
/****** globals ******/
int n; // size of omega = {1, ,n}
int r; // number of generators
Permutation* g; // the generators
int cosreps = 0; // number of (= size of orbit of alpha)
Permutation* cosrep; // coset representatives (to store output of SchreierTree)
Permutation gen (maxn,'i'); // group element moving original alpha to actual alpha
/****** ScheierTree ******/
void ScheierTree(int alpha) { // depth first search to determine the orbit of alpha
static int ag; // image of actual alpha under generator g[j]
cosrep[alpha] = gen; cosreps++;
for (int j=0;j<r;j++) { ag = g[j].p[alpha];
if (!cosrep[ag].isdefined()) {
gen *= g[j]; ScheierTree(ag); gen *= g[j].inverse();
}
}
}
/****** main ******/
int main () {
cout << "n, num generators, generators ? "; cin >> n;
g = new Permutation[n] (n,'u');
cosrep = new Permutation[n] (n,'u');
gen.n = n;
cin >> r;
for (int j=0;j<r;j++) g[j].input();
// get the coset representatives for G(alpha)
ScheierTree(0);
cout << "Wow, it worked";
//for (int i=0;i<n;i++) cosrep[i].output();
delete[] g; delete[] cosrep;
return 0;
}