I am interested to find out what is the memory limit for the automatic and dynamically-allocated variables, so I tested things like that:
int main() {
const int N = 1000000;
int a[N];
a[1] = 100;
}
I found the maximum N that would not incur a Segmentation fault is 2,600,000, about 10MB.
Then I tested dynamically allocated variables, like this:
int main() {
const int N = 1000000;
int* a = new int [N];
delete[] a;
}
I found that maximum N that would not throw an exception is about 730,000,000, that’s about 3GB.
Now the question is, how is the 10MB limit (for automatic variables) and 3GB limit (for dynamically-allocated variables) determined. I assume it is related to my machine? Also, is there any way to increase the limit, in case I really need it?
The limit for automatic variables is the amount of memory allocated for the machine stack. 10MB is actually rather high; 1 or 2 MB is a more common default.
Obviously, the 3GB is the OS limit — it’s roughly the size of the process space allowed by the OS to a program. It’ll vary widely by OS and hardware platform.