stack overflow error when using vector stl

Discussion in 'C++' started by capes, Sep 29, 2006.

  1. capes

    capes Guest

    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);
    }
     
    capes, Sep 29, 2006
    #1
    1. Advertising

  2. capes

    Daniel T. Guest

    "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!!!!!


    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.
     
    Daniel T., Sep 29, 2006
    #2
    1. Advertising

  3. capes

    Guest

    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, 2006
    #3
  4. 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.
     
    Jacek Dziedzic, Sep 30, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    508
  2. Replies:
    0
    Views:
    412
  3. Replies:
    8
    Views:
    1,962
    Csaba
    Feb 18, 2006
  4. Replies:
    1
    Views:
    435
    Peter Bromberg [C# MVP]
    Mar 17, 2008
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    275
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page