As @PaulR already said in the comments your problem is a stack overflow. You can solve this problems a few ways. If you're categorically against changing your code then you can adjust the stack size in your compiler. How exactly that is done depends on your compiler (gcc example and VS example). You can also adjust it at run-time but that's an OS dependent function, so I'm not going to go into details about that.
The solution that I would prefer is allocate the memory on the heap. This can be done by either allocating the arrays on the heap int **left = new int*[n1];
but then you have to remember to call delete []left;
when you don't need the memory anymore. The far more elegant solution would be to use a std::vector
instead.
I challenge you to compile this with all optimizations on and tell me that it's significantly slower than your code.
int k=698900;
int n1=k/2;
int n2=k/2;
//not that I condone storing raw pointers in a vector
//admittedly this is a rather dangerous thing to do
//storing indices would be smarter because upon adding
//elements the memory could be relocated
std::vector<int *> left(n1);
std::vector<int *> right(n2);
std::vector<int> list(k);
for(int i=0; i<k; i++){
list[i] = i;
}
for(int i=0; i<n1; i++){
left[i] = &list[i];
}
for(int i=0; i<n2; i++){
right[i] = &list[i];
}
for(int i=0; i<n2; i++){
cout << *right[i] << endl;
}
Like @PaulR mentioned you can also allocate the arrays statically (static int *left[n1];
) or globally (outside of any function or class declaration/definition)