This is because your array is larger than the stack size. Thus, your program crashes when it tries to call something new during a function call.
You will get conceptually the same thing as a stack overflow, except that the local variable is incredibly large, and not too many function calls.
The stack is a small area of โโmemory for use by household functions and local variables. It was never big, just a few megabytes. This is why you need dynamic allocation to get rid of your problem. Most dynamic allocations will concern the heap, which is often limited only by your physical memory.
You will need to allocate an array on the heap. You have several options for this, the simplest of which is probably using std::vector<int>
. They behave in much the same way as regular arrays, and their storage is automatically controlled, so this should not be a problem.
#include <vector> #include <iostream> int PROB_SIZE = 10000000; using namespace std; int main() { vector<int> numbers(PROB_SIZE); cout << "Generating " << PROB_SIZE << " random numbers... " << flush; return 0; }
zneak source share