Segmentation Fault in C++

Discussion in 'Software' started by deltaone, Aug 2, 2006.

  1. deltaone

    deltaone

    Joined:
    Aug 2, 2006
    Messages:
    1
    Hi Guys,

    I have a problem to calculate the diffusion from a cell in a grid cell layout. I am passing the value from 0 to n as (x,y), nx is no of cell in the grid in x direction.

    geoCellCurrent is a vector of Geographic Region Class....

    geoCellCurrent[nx*(x-1)+(y-1)] might not exist if it goes beyond the index range.

    index = nx * (x-1) + (y-1)

    i guess i am getting segmentation fault because of this problem

    Is there any way where i can ignore this object c*State or v*State if index goes beyond range...and it cannot be used further in the equation of diff_M


    Here is the code
    double GeographicRegion::getDiffusionM(unsigned int x,unsigned int y)
    {

    CellState& c1State = geoCellCurrent[nx*(x-1)+(y-1)].c1State;
    VectorState& v1State = geoCellCurrent[nx*(x-1)+(y-1)].v1State;

    CellState& c2State = geoCellCurrent[nx*(x-1)+y].c2State;
    VectorState& v2State = geoCellCurrent[nx*(x-1)+y].v2State;

    CellState& c3State = geoCellCurrent[nx*(x-1)+(y+1)].c3State;
    VectorState& v3State = geoCellCurrent[nx*(x-1)+(y+1)].v3State;

    CellState& c4State = geoCellCurrent[nx*x+(y-1)].c4State;
    VectorState& v4State = geoCellCurrent[nx*x+(y-1)].v4State;

    CellState& c5State = geoCellCurrent[nx*x+(y+1)].c5State; //Cell whose concentratxon is caculated
    VectorState& v5State = geoCellCurrent[nx*x+(y+1)].v5State;

    CellState& c6State = geoCellCurrent[nx*(x+1)+(y-1)].c6State;
    VectorState& v6State = geoCellCurrent[nx*(x+1)+(y-1)].v6State;

    CellState& c7State = geoCellCurrent[nx*(x+1)+y].c7State;
    VectorState& v7State = geoCellCurrent[nx*(x+1)+y].v7State;

    CellState& c8State = geoCellCurrent[nx*(x+1)+(y+1)].c8State;
    VectorState& v8State = geoCellCurrent[nx*(x+1)+(y+1)].v8State;

    CellState& c9State = geoCellCurrent[nx*(x+1)+(y+1)].c9State;
    VectorState& v9State = geoCellCurrent[nx*(x+1)+(y+1)].v9State;

    double outRate1 = diffTabM[c5State.getClassification()][c1State.getClassification()];
    double inRate1 = diffTabM[c1State.getClassification()][c5State.getClassification()];

    double outRate2 = diffTabM[c5State.getClassification()][c2State.getClassification()];
    double inRate2 = diffTabM[c2State.getClassification()][c5State.getClassification()];

    double outRate3 = diffTabM[c5State.getClassification()][c3State.getClassification()];
    double inRate3 = diffTabM[c3State.getClassification()][c5State.getClassification()];

    double outRate4 = diffTabM[c5State.getClassification()][c4State.getClassification()];
    double inRate4 = diffTabM[c4State.getClassification()][c5State.getClassification()];

    double outRate6 = diffTabM[c5State.getClassification()][c6State.getClassification()];
    double inRate6 = diffTabM[c6State.getClassification()][c5State.getClassification()];

    double outRate7 = diffTabM[c5State.getClassification()][c7State.getClassification()];
    double inRate7 = diffTabM[c7State.getClassification()][c5State.getClassification()];

    double outRate8 = diffTabM[c5State.getClassification()][c8State.getClassification()];
    double inRate8 = diffTabM[c8State.getClassification()][c5State.getClassification()];

    double outRate9 = diffTabM[c5State.getClassification()][c9State.getClassification()];
    double inRate9 = diffTabM[c9State.getClassification()][c5State.getClassification()];

    diff_M = outRate1*v5State.getVectorStateM() - inRate1*v1State.getVectorStateM() + outRate2*v5State.getVectorStateM() - inRate2*v2State.getVectorStateM() + outRate3*v5State.getVectorStateM() - inRate3*v3State.getVectorStateM() + outRate4*v5State.getVectorStateM() - inRate4*v4State.getVectorStateM() + outRate6*v5State.getVectorStateM() - inRate6*v6State.getVectorStateM() + outRate7*v5State.getVectorStateM() - inRate7*v7State.getVectorStateM() + outRate8*v5State.getVectorStateM() - inRate8*v8State.getVectorStateM() + outRate9*v5State.getVectorStateM() - inRate9*v9State.getVectorStateM();

    cout << " Male Diffusion " << diff_M << endl;
    return diff_M;
    }
     
    deltaone, Aug 2, 2006
    #1
    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. Keith Lee
    Replies:
    3
    Views:
    2,547
    Grzegorz Karwowski
    Apr 29, 2006
  2. sakura

    Segmentation fault in C (fclose)

    sakura, Aug 31, 2006, in forum: Software
    Replies:
    1
    Views:
    1,554
    Alain
    Oct 20, 2006
  3. acumind@gmail.com

    Segmentation fault in 'accept'

    acumind@gmail.com, Aug 16, 2007, in forum: Computer Support
    Replies:
    0
    Views:
    929
    acumind@gmail.com
    Aug 16, 2007
  4. bhavinvyas

    I am getting segmentation fault - WHY?

    bhavinvyas, Dec 31, 2008, in forum: Software
    Replies:
    0
    Views:
    918
    bhavinvyas
    Dec 31, 2008
  5. Peter Potamus the Purple Hippo

    Re: Segmentation Fault

    Peter Potamus the Purple Hippo, Nov 29, 2009, in forum: Firefox
    Replies:
    3
    Views:
    1,694
    Leonidas Jones
    Dec 2, 2009
Loading...

Share This Page