Segmentation fault when you have too large of an array

It appears that in cpp if you initialize a semi-large array (that has several million elements) and then try to access the values, you end up with a segmentation fault during runtime.
I’ve determined the upper limit (at least on my computer) to be around 2.1 million elements using (manual) binary searching, but I feel like the limit is way too low, especially if you’re going to use an array to store or calculate values on USACO problems. Is there a workaround, or is the use of arrays not encouraged in cpp?

Here’s the code that I used to test this. (this throws a seg fault on my computer)

int main(){
	int test[2100000];
	for(auto& val: test){
		cout<<val;
	}
}

Thanks

2 Likes

Cool, thanks!