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
-
2+1 for asking about stack overflow on stack overflowFoo Bah– Foo Bah2011-09-20 04:49:15 +00:00Commented Sep 20, 2011 at 4:49
-
after reading the title of this question, for once I thought its a recursive question! :PVineet Menon– Vineet Menon2011-09-20 04:49:49 +00:00Commented Sep 20, 2011 at 4:49
-
What exactly do you mean by "fill up main memory with allocated arrays"? If you need to do the allocating yourself, malloc answer below is the right direction, but I thought maybe the assignment is asking for something odd...J Trana– J Trana2011-09-20 05:05:29 +00:00Commented Sep 20, 2011 at 5:05
Add a comment
|
1 Answer
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.