Reputation: 21
I have an assignment where I need to basically fill up main memory with allocated arrays in C. I am using VS2010 and kept on receiving stack overflow errors. Increasing the stack reserve past its default 1 MB helped, however now the array size I am working with is even larger and it seems no matter how much I increase the reserve, it now continually gives me a stack overflow error. any help would be appreciated. -thanks
Upvotes: 2
Views: 281
Reputation: 471567
You're probably allocating your arrays on the stack. That's why you're getting stack overflows since your stack is never gonna be as large as your entire main memory.
You need to use malloc()
to create arrays on the heap. That will allow you to use up all the main memory.
In other words, you can't do this:
int array[1000000];
That will certainly blow your stack. You need to do this:
int *array = malloc(1000000 * sizeof(int));
and you need to eventually free it like this:
free(array);
Otherwise you will get a memory leak.
Upvotes: 3