stack overflow - urgent

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;

struct firmType
{
....
}

struct bankType
{
.....
}

struct economy
{
firmType firms[I];
bankType banks[Z];
double B[Z][I][D];
}

int main()
{
economy * econptr = new economy[T];

.....

delete [] econptr;
return 0;
}



}



seems like you are trying to allocate a very large contiguous block of memory for the econptr array.

the system may not have such a large contiguous block available to allocate for this array.

if you change your arrays to vectors then you will most likely not have the stack overflow error.
Thank you very much, but I am not very good at vectors. Could you show me how?
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.

Last edited on
So wouldn't it give the stack overflow error when the size of the vector will reach to 1000?
I don't see why that would help. He is creating the array dynamically already.
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <vector>


using namespace std;



struct firmType
{

};


struct bankType
{

};


struct economy
{
   vector<firmType>                 firms;
   vector<bankType>                 banks;
   vector<vector<vector<double> > > B;
};
and in main function:

 
vector<economy>      vecEcon;


in place of [econptr]
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"
To SIK: and How can I add elements to the vectors.

I mean, I want to study with 400 firms, 40 banks and I want to make the length of the vector<economy> vecEcon to be 1000.
How much memory does your system have available? Something in your code is trying to use more memory than you have.
Last edited on
@ilke: the vector is a dynamic array - it can maintain it's own size unlike a conventional array.

to add elements to the vector you have the following options:

1
2
3
4
5
6
7
8
9
10
11
12
   economy e1;
   economy e2;
   economy e3;

   // does automatic resizing
   vecEcon.push_back(e1);
   vecEcon.push_back(e2);

   // Alternativley - allocate 400 items upfront --> better performance
   vecEcon.resize(400);
   vecEcon[2] = e3;


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);
Last edited on
@clanmjc

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.
Last edited on
struct firmType
{
float ai;
float lev;
float pm;
float rstar;
float bstar;
float K;
float Y;
float pri;
float p;
float li;
bool bankturp;
};



and


struct bankType
{
float L;
float D;
float az;
float res;
float car;
float prz;
};
'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).
If you build with debug symbols, it will be able to tell you which line of code is actually requesting more memory than exists.
Topic archived. No new replies allowed.