开发者

segmentation fault when calling a function

Segmentation fault when calling the Update_Multiplier and gdb debugger shows these:

Program received signal SIGSEGV, Segmentation fault. 0x080b74e8 in Update_Multiplier() ()

double upperbound = 116325;
double objective = 1.1707e+07;
int main()
{
    Update_Multiplier();
}
void Update_Multiplier()
{
    cout << "function 0" << endl;
    // Determine subgradient vectors
    double gra[1000][1000];
    double grb[1000][1000];
    double dumX = 0;
    double stepsize[1000][1000];
    double tuning=2;
    double LRADum[1000][1000];
    double LRBDum[1000][1000];

    cout << "function 1" << endl;
    // update subgradient vectors
    for (int i=1; i<=noOfNodes; i++)
    {
        for (int j=1; j<=noOfNodes; j++)
        {
            if (C[i][j] != 0)
            {
                dumX=0;
                for (int p=1; p<=noOfCommodity; p++)
                {
                    dumX += X[i][j][p];
                }
                gra[i][j]=dumX-U[i][j]*Y[i][j]-Q[i][j];
                grb[i][j]=Q[i][j]-B[i][j]*Y[i][j];
            }
        }
    }

    // update stepsize
    cout << "function 2" << endl;
    for (int i=1; i<=noOfNodes; i++)
    {
        for (int j=1; j<=noOfNodes; j++)
        {
            if (C[i][j] != 0)
 开发者_开发问答           {
                stepsize[i][j]=(tuning*(UpperBound-Objective))/sqrt((gra[i][j]*gra[i][j])*(grb[i][j]*grb[i][j]));
                LRADum[i][j]=LRA[i][j]+stepsize[i][j]*gra[i][j];
                LRA[i][j]=LRADum[i][j];
                LRBDum[i][j]=LRB[i][j]+stepsize[i][j]*grb[i][j];
                LRB[i][j]=LRBDum[i][j];

            }
        }
    }

}


I see two suspicious things in your code.

First, you are taking too much stack space (about ~40 MB).
Second, you are starting the index of the array at 1, where it should be 0:

for (int i=1; i<=noOfNodes; i++)

Change it to:

for (int i=0; i<noOfNodes; i++)


At a guess, you have a stack overflow! You cannot reliably create gigantic arrays on the stack. You need to create them dynamically or statically.


Where did you define noOfNodes? What is the initial value of this? Or, do you read this in from the console? If this is uninitialized, it probably has junk data -- which may or may not explain the crash.


You need a stack of at least 40 megabytes to run this function because you're allocating five arrays of one million eight-byte doubles each.

Change the function to allocate the double arrays from the heap using new.


You should really give us the whole code, e.g. noOfNodes is not defined anywhere.

Just a stab in the dark: are you possibly overflowing C since your indices (i and j) go from 1 to noOfNodes?


First, what Neil said is true.

Second, C and C++ arrays start from index zero. If you declare

int a[100]; // 100 elements, from zeroth to ninety-ninth.

Then its elements are a[0], a[1] ... a[99].


I can not see anything wrong with this code as given, BUT: You might have an off-by-one error if noOfNodes is 1000.

Remember that Arrays are 0-indexed so you have to access indexes 0 - 999 instead of 1 - 1000 as you are doing


me too i have this problem and my function returned std::string now i just do reference an dreturn type void like that:

void readOnDicoFile(std::ifstream file/*If you have "using namespace std;" instruction, put ifstream, not std::ifstream (you can put both)*/)

and before:

std::string readOnDicoFile(std::string fileName/*If you have "using namespace std;" instruction, put ifstream, not std::ifstream (you can put both)*/)        
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜