stack overflow error when using vector stl

C

capes

Hi,
I have a structure called Tissue and I use it in a matrix like this:

vector < vector < Tissue* > > tissueArray(cRows);

This whole function works great for a matrix size of 2X2 to 15X15.
Beyond that it crashed with a STATUS_STACK_OVERFLOW exception!!!
1. Why does his happen?
2. How do I fix it?
3. At some point I may go to a 300X300 array. What happens then?

Thanks a lot for your time and attention!!!!!

Here are the details of my code
1.
This is the Tissue struct:
struct TissueStruct
{
TissueType type; // fibre or vessel
TissueStimulus stimulus;
TissueState state;
double tpo2;
double po2;
double deficit;
double demand;
double x;
double y;
double v;
double u;
double Iext;

};

2.
I populate the tissueArray matrix (or vector of vector of Tissue) with
a init dunction like this:

for (int i = 0; i < cRows; i++)
{
//creating a row of tissue cells
vector <Tissue*> trow(cCols);
for (int j=0; j<cCols; j++)
{

if (type == Fibre)
{
trow.push_back(createFibre());
type = Vessel;
}
else
{
trow.push_back(createVessel());
// cout << "Type Vessel " << j << " " << trow[j]->type << endl;
type = Fibre;
}
}
// cout << trow.size() << endl;
tarray.push_back(trow);
}
}

3.
The createFibre and createVessel functions do the actual memory
allocation with the new keyword:

Tissue* createFibre()
{
Tissue* tissue;
tissue = new (Tissue);
tissue->type = Fibre;
tissue->po2 = Fpo2;
tissue->state = Inactive;
tissue->stimulus = Absent;
tissue->deficit = tissue->demand = 0.0;
tissue->x = tissue->y = tissue->Iext = tissue->v = tissue-> u =
tissue->tpo2= 0.0;
return (tissue);
}
 
D

Daniel T.

capes said:
Hi,
I have a structure called Tissue and I use it in a matrix like this:

vector < vector < Tissue* > > tissueArray(cRows);

This whole function works great for a matrix size of 2X2 to 15X15.
Beyond that it crashed with a STATUS_STACK_OVERFLOW exception!!!
1. Why does his happen?
2. How do I fix it?
3. At some point I may go to a 300X300 array. What happens then?

Thanks a lot for your time and attention!!!!!

I don't see how any of the code you posted would cause a stack overflow.
Try to post a complete, compilable example of your error.
 
R

ravinderthakur

this seems to be some platform specific issue. All the local objects
are created on the stack and each os has some size allocated to the
stack. So when the number of objects that u create can no longer fit in
the stack, theoretically it can overflow and give the stack overflow
error.


u can instead try creating the objects on the heap and work with the
pointers to object rather than the objects itself.


thanks
ravinder thakur
 
J

Jacek Dziedzic

capes said:
Hi,
I have a structure called Tissue and I use it in a matrix like this:

vector < vector < Tissue* > > tissueArray(cRows);

This whole function works great for a matrix size of 2X2 to 15X15.
Beyond that it crashed with a STATUS_STACK_OVERFLOW exception!!!
1. Why does his happen?
2. How do I fix it?
3. At some point I may go to a 300X300 array. What happens then?

Thanks a lot for your time and attention!!!!!

Here are the details of my code
1.
This is the Tissue struct:
struct TissueStruct
{
TissueType type; // fibre or vessel
TissueStimulus stimulus;
TissueState state;
double tpo2;
double po2;
double deficit;
double demand;
double x;
double y;
double v;
double u;
double Iext;

};

2.
I populate the tissueArray matrix (or vector of vector of Tissue) with
a init dunction like this:

for (int i = 0; i < cRows; i++)
{
//creating a row of tissue cells
vector <Tissue*> trow(cCols);
for (int j=0; j<cCols; j++)
{

if (type == Fibre)
{
trow.push_back(createFibre());
type = Vessel;
}
else
{
trow.push_back(createVessel());
// cout << "Type Vessel " << j << " " << trow[j]->type << endl;
type = Fibre;
}
}
// cout << trow.size() << endl;
tarray.push_back(trow);
}
}

3.
The createFibre and createVessel functions do the actual memory
allocation with the new keyword:

Tissue* createFibre()
{
Tissue* tissue;
tissue = new (Tissue);
tissue->type = Fibre;
tissue->po2 = Fpo2;
tissue->state = Inactive;
tissue->stimulus = Absent;
tissue->deficit = tissue->demand = 0.0;
tissue->x = tissue->y = tissue->Iext = tissue->v = tissue-> u =
tissue->tpo2= 0.0;
return (tissue);
}

Well, all your vectors store pointers, which should only
occupy a few (usually 4) bytes each. A stack's size is
usually in the order of a few MB, so you should be able
to store about a million of such pointers without a problem
(the actual data winds up on the heap, not on the stack).

Try adding some debug statements that would show how many
times the loop iterates, like

cout << "adding an element\n";

before every push_back()... perhaps it'll turn out that
your loop is infinite, that would explain the stack overflow.

HTH,
- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top