 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
bios Guest
|
Posted: Sat Sep 30, 2006 12:01 am Post subject: stack overflow error when using vector stl |
|
|
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);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Carlos Moreno Guest
|
Posted: Sat Sep 30, 2006 3:19 am Post subject: Re: stack overflow error when using vector stl |
|
|
bios wrote:
| Quote: | 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?
|
I don't see how it wuld happen with the code you posted.
Are you using recursion by any chance?
Could you post complete demo that exhibits the problem?
(perhaps it is something else that's causing the stack
overflow).
The typical reasons are that you allocate auto arrays
too large (auto arrays meaning a conventional, C-style
array as a local variable of a (perhaps recursive)
function.
| Quote: | 2. How do I fix it?
|
I know my answer is going to sound silly, but: you
fixing by finding the cause and removing it. It really
boils down to finding out why the stack would overflow
(with the partial code you posted, I don't see a possible
reason)
The one straneg detail that I see in your code (which would
*not* be by itself the cause of a stack overflow) is that
you give your vector a size (the vector of vectors), and
then when you populaet it you use push_back.
If you have a 10x10 matrix, then you are declaring a
vector of 10 vectors. You then *append* ten more
vectors (the ten populated rows). You end up with an
array of 20 x 10, and the first 10 rows are empty...
If you then access the matrix with a nested loop of
10 x 10 assuming that that is the correct size, you
would end up accessing elements outside the boundaries
of the vector. That invokes undefined behaviour,
which means that *anything* could happen -- perhaps
the anything in your case happens to be falling in
some sort of infinite loop that ends up causing a
stack overflow?
Carlos
--
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Terry G Guest
|
Posted: Sat Sep 30, 2006 3:27 am Post subject: Re: stack overflow error when using vector stl |
|
|
I modified your code to get it to compile and it ran fine.
The one flaw I noticed was that you are initializing your vectors with:
vector< vector <Tissue*> > tissueArray(cRows);
and later,
vector<Tissue*> tRow(cCols);
I don't think you want to do that, since you're using push_back().
Below, I leave off the ctor argument, and use vector::reserve() to set the
initial capacity.
#include <iostream>
#include <vector>
using namespace std;
enum TissueType { Fibre, Vessel };
enum TissueStimulus { Absent, Present };
enum TissueState { Inactive, Active };
const double Fpo2 = 0.0;
struct Tissue {
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;
}; // Tissue
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);
} // createFibre
Tissue* createVessel() {
Tissue* tissue;
tissue = new (Tissue);
tissue->type = Vessel;
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);
} // createVessel
int main() {
const int cRows = 300;
const int cCols = 300;
TissueType type = Vessel;
vector < vector < Tissue* > > tissueArray;
tissueArray.reserve(cRows);
for (int i = 0; i < cRows; i++) {
//creating a row of tissue cells
vector <Tissue*> trow;
trow.reserve(cCols);
for (int j=0; j<cCols; j++) {
if (type == Fibre) {
trow.push_back(createFibre());
type = Vessel;
}
else {
trow.push_back(createVessel());
type = Fibre;
}
}
tissueArray.push_back(trow);
}
cout << "Everything is fine here." << endl;
return 0;
} // main
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|