By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

stack overflow error when using vector stl

P: n/a
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);
}

Sep 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"capes" <ka*********@gmail.comwrote:
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.

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Sep 29 '06 #2

P: n/a
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

Sep 29 '06 #3

P: n/a
capes wrote:
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.
Sep 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.