Hi,
I have written following data for my thesis and yet it gives stack overflow error. Could anyone can help please? is there a way to handle this problem???
const int I = 300;
const int Z = 30;
const int T = 400;
const int D = 5;
vector<economy> econVector; // create a vector of type economy
Off-topic chat about stack size considerations:
Looking at your code, it seems that each economy object contains 45000 double objects (ignoring the firmType and BankType objects). If each of those is four bytes, that's around 180000 bytes. You're trying to make 400 of these, so you're trying to allocate around 72000000 bytes, or roughly 72 megabytes.
Typical default stack size on a consumer Windows OS machine is one megabyte, if I recall correctly.
Are you sure the problem is with the code you have shown above? Do you get a stack overflow if you run the program with the ..... code inside main removed?
Yes. When I make T = 30 for example, program properly works. But when I make it 200 or more it gives stack overflow. And sometimes it also says:
"exception: std::bad_alloc at memory location 0x001ff658"
you can basically handle vectors in same way as convetional arrays, however, you should learn the std components used with vectors as these usually give much better performance.
for instance - you could traverse a vector in same way you do for conventional array like:
1 2 3 4
for (int i=0; i<vecEcon.size(); i++)
{
SomeMethod(vecEcon[i]);
}
this is not preferred and is slower than:
1 2 3 4 5
for (vector<economy>::iterator it=vecEcon.begin(); it != vecEcon.end(); it++)
{
SomeMethod(*it);
}
72 megabytes shouldn't be a problem these days when allocated on the heap. So I'd say that you're problem is something else. Like read/write data out of bounds
What is firmType and bankType? Are they structs as well, enums, ints, some object?
If double is 8 bytes on your system, not including firmType/bankType or padding, then your looking at 144MB. This is not much, but without knowing, the size of those struct and or how much ram your system has it's hard to know. I'm able to allocate 1.4GB worth of memory using the code you've posted (Making up my own firmType/bankType) by declaring arrays of 9 int for each of those. I don't crash until I create 4500 size economy. I'm running on Windows server 2003 with 16GB of Ram.
this is not preferred and is slower than:
And this is an alternate method that may be more efficent: std::for_each(vecEcon.begin(),vecEcon.end(),SomeMethod);
firmType and bankType are structs and contain some float variables.
How could you do that? Could you please tell me?
I am running a 64 bit Windows 7 with 8 GB RAM.
Post these structs please. Also, please run your program, post the exact error message/exceptions and where in the code this takes place as well as if there are other allocations before the crash.
'phd_10101516.exe': Loaded 'C:\Users\ILKER\Documents\Visual Studio 2010\Projects\phd_10101516\Debug\phd_10101516.exe', Symbols loaded.
'phd_10101516.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'phd_10101516.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'phd_10101516.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'phd_10101516.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'phd_10101516.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
First-chance exception at 0x753cc41f in phd_10101516.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x004bfa8c..
Unhandled exception at 0x753cc41f in phd_10101516.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x004bfa8c..
The program '[2264] phd_10101516.exe: Native' has exited with code -529697949 (0xe06d7363).