views:

149

answers:

7

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];

            }
        }
    }

}
+3  A: 

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.

anon
+3  A: 

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.

dirkgently
+7  A: 

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++)
AraK
heh.. beat me to it. Although the large stack is a possibility, I think this is the most likely scenario.
Randolpho
The comments about the loops starting at 1 are also valid, but we don't know the value of noOfNodes. If it's less than 1,000 then that part at least will be fine. But what if it's greater than 1,000? It looks like the function is just allocating 1,000 x 1,000 arrays with the assumption that noOfNodes will never be that large. It should use the actual value of noOfNodes and allocate them dynamically from the heap.
Willis Blackburn
@Willis Blackburn: Even if noOfNodes is set to the exact number of items in the array, starting at 1 and using `i<=noOfNodes` will segfault.
Randolpho
For that matter we don't know what B, C, Q, U, X, Y, or noOfCommodity are either.
Willis Blackburn
Randolpho: That's why I said "If it's *less than* 1,000 ..." :-)
Willis Blackburn
Okay I should also have said "*greater than or equal to* 1,000" ...
Willis Blackburn
+2  A: 

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.

Willis Blackburn
+1 Ya it is ~40 MB. I first mentioned it is ~5 MB in my post :)
AraK
+1  A: 

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?

liwp
A: 

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].

Eduardo León
A: 

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

dbemerlin