stack overflow error when using vector stl

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • capes

    #1

    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(cRo ws);

    This whole function works great for a matrix size of 2X2 to 15X15.
    Beyond that it crashed with a STATUS_STACK_OV ERFLOW 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(cC ols);
    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_bac k(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);
    }

  • Daniel T.

    #2
    Re: stack overflow error when using vector stl

    "capes" <kanakapriya@gm ail.comwrote:
    Hi,
    I have a structure called Tissue and I use it in a matrix like this:
    >
    vector < vector < Tissue* tissueArray(cRo ws);
    >
    This whole function works great for a matrix size of 2X2 to 15X15.
    Beyond that it crashed with a STATUS_STACK_OV ERFLOW 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.

    Comment

    • ravinderthakur@gmail.com

      #3
      Re: stack overflow error when using vector stl

      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

      Comment

      • Jacek Dziedzic

        #4
        Re: stack overflow error when using vector stl

        capes wrote:
        Hi,
        I have a structure called Tissue and I use it in a matrix like this:
        >
        vector < vector < Tissue* tissueArray(cRo ws);
        >
        This whole function works great for a matrix size of 2X2 to 15X15.
        Beyond that it crashed with a STATUS_STACK_OV ERFLOW 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(cC ols);
        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_bac k(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.

        Comment

        Working...