I am running a simulation program in VC++ 6 that requires arrays (dynamic or otherwise) that will eventually have as many as 200,000 entries of type "double".
The program works fine when I use small arrays (with a few hundred entries) but fails when I increase the array size to say several thousands. The error code is "Unhandled exception in sim.exe: 0xC00000FD: Stack Overflow".
I guess the main question is, if possible at all, how do I increase the stack or heap size so that the program can accomodate arrays with 200,000 "double" entries Other advice most welcomed.
Thanks in advance!

Heap/Stack Overflow/Overrun Issues
Athol Marshall
You do realise that the VC2005 implimentation of C++ is far superior The IDE is a little different, but it's worth the work.
Brian Rogers
Switching to new and delete will internally call malloc and free, but it will improve both the readability of your code, and will avoid errors where you call new and then free, or the other way around. I'd suspect your error is in here somewhere, given that there's no reason I can see why you should have trouble working with 200000 doubles.
The Code Monkey
> Specifically, I can declare and allocate arrays (of size 200,000) without a problem. The problem arises when I pass them to functions.
How are you passing these arrays Can you show an example
DarrelC
Note that the stack is not used for data only. It is also used for internal data related to function calls. Therefore you can obtain Stack Overflow error in case of too many recursive calls. This situation probably is more difficult to solve since it requires reconsidering of the algorithm.
Eric Geil
I really hope "pass them to functions" doesn't mean that you're trying to pass the huge array by value, i.e. pass a complete copy of the array to the called function.
alexvi
I'd prefer to take the bull by the horns and get to the bottom of things.
It'll be great if you could point to me how to use "the debugger and recording the value of the ESP register between the moment of allocation and the moment of stack overflow".
I plan to work on the heap, rather than off the stack, as it seems more reasonable to put a 200,000 doubles array on the heap.
I have access to VS 2005... and I can migrate to it if necessary. Familiarity with VC 6 is the main reason why I am staying put.
dolot
If you have access to VS2005, you will thank yourself for moving up. As Cgraus says, the C++ is conformant, and the IDE is more functional.
Working off the heap is a requirement in your case. There's not enough stack space to hold an array of your size.
In the best case, the debugger will show you where you might be eating up stack space to hold a large array (i.e. easy fix). In the worst cases, you have an obscure memory corruption issue that will be harder to track down, or perhaps there is something clobbering the stack pointer register, or its something else beyond straightfoward debugging can detect.
I'm assuming that the calloc lines and the "errant" function call where the stack overflows both are in the same function. If not, you'll have to infer what to do based on my instructions here.
1. Set a breakpoint at the point of calloc and run (F5, hoping that the keyboard mapping hasn't changed since VC6).
2. Open the registers window and the callstack window. In the registers window, record the value of ESP, which will be a hexademical value that measures the current stack position. Stacks grow in the negative direction, so any delta from this needs to be computed accordingly.
3. Using F10, step line by line up to the function that causes the function. Verify that ESP hasn't changed yet.
4. Step into the "errant" function with F11. Check the ESP pointer and compute the delta with respect to step 2. It should have changed a little (4-8 bytes per argument, size of callee stack frame, etc).
5. If it changed a lot (overflowed), rerun you app, this time showing disassembly before stepping into the function . See if you can find the exact instruction where the ESP goes ape on you.
6. In the case where the ESP never goes ape on you yet you still get the stack overflow exception, you should continue combing your code with the debugger looking for clues.
Brian
CyberCipher
// A = (int *)calloc((2*e*t)*3, sizeof(int)); //e=1600, t=20
This is C, you should use C++ when programming in C++. That means using new and delete.
Hong Zhang
I can create an array of 200,000 doubles in VC6 without any trouble. The problem is probably in your actual code. VC6 support has ended, and VC6 questions are off topic here, I recommend www.codeproject.com as an alternative.
Abu Islah
> am running a simulation program in VC++ 6 that requires arrays (dynamic or otherwise)
How are you allocating your array exactly
If you're allocating it on the stack, move it to the heap (use new in C++ or malloc in C). If it already on the heap, then the problem isn't the stack.
You can increase the stack size via the /STACK linker flag. But 200,000 * 8 bytes on the stack is way beyond reasonable. (There are even limits to the size of the frame, which I believe the compiler normally catches on its own).
Brian
Raman-astix
How do you do,Brian Kramer
I encountered the same problem,0xC00000FD: Stack Overflow.My current task is to process XML retrieval results and those data are rather huge though after dimension reduction.I used arrays to store these vectors,some two-dimension ones are 810*100,231*100,26*100.At the same time,there are also struct type arrays.
Since the error message is 0xC00000FD: Stack Overflow,may the problem come from heap I just loaded the data from secondary memory to main memory and used the data in the main memory to initialize another struct type array.When I took the first step,it run well.However,while I was trying to initialize the array,there came the error message.I didn't use new or malloc.How can I judge where the problem came,from stack or heap
You advised to increase the stack size via the /STACK linker flag.But you seemed to disagree to allocate large size on the stack.Any other excellent methods Or just say more details about changing /STACK linker flag.
Thanks very much.
Jack
CSH100885
Okay, I will move my VC6-specific post to other websites.
Specifically, I can declare and allocate arrays (of size 200,000) without a problem. The problem arises when I pass them to functions.
I am using malloc and calloc to allocate the larger arrays. I put the smaller ones on the stack. For example, I use:
A = (int *)calloc((2*e*t)*3, sizeof(int)); //e=1600, t=20
to allocate an array.
One problem I face when using malloc() and calloc() is that the free() command fails when I compile and run the program in debug mode. Does anybody face the same problem
Artix
Thank you so much for such a speedy response...
Do you think switching to "new" and "delete" will help I grew up on C so calloc/free are the functions I am more used to.
Here is an example....
A = (int *)calloc((2*e*t)*3, sizeof(int)); //e=1600, t=20
A_1 = (int *) calloc((2*e+1)*e*t*3, sizeof(int));
A_2 = (int *)calloc((2*e*t)*3, sizeof(int));
.
recv_func(data_ptr, e, t, R, A, A_1, A_2, &B, C);
What baffles me is that it works perfectly when the arrays are small e.g. several hundred entries but fails when the size goes up to several thousand.
Mark Woodlief
I think at this point you need to attack the problem head on by using the debugger and recording the value of the ESP register between the moment of allocation and the moment of stack overflow.
I can provide more info about how this might be done, but if you do so bear in mind that I don't have VC6 installed.
Brian