3

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
  • Hmmm, possibly 40MB of stack space required. What's the platform, and do you know how much memory is allocated for the stack? Commented Jan 22, 2010 at 14:53
  • I think he's programming one of those computers with 256-bit processors, ~200TB RAM that nobody else knows. Commented Jan 22, 2010 at 15:00
  • To be honest, it's pretty surprising that it even gets to segfault. I can't see declarations of cout, endl, noOfNodes, C, noOfCommodity, X, U, Y, Q, B, UpperBound, Objective, sqrt, LRA, LRB. In addition there's no declaration of Update_Multiplier in scope where it's called from main. Commented Jan 22, 2010 at 15:04

8 Answers 8

7

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++)
Sign up to request clarification or add additional context in comments.

5 Comments

heh.. beat me to it. Although the large stack is a possibility, I think this is the most likely scenario.
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: Even if noOfNodes is set to the exact number of items in the array, starting at 1 and using i<=noOfNodes will segfault.
For that matter we don't know what B, C, Q, U, X, Y, or noOfCommodity are either.
Randolpho: That's why I said "If it's less than 1,000 ..." :-)
4

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.

Comments

3

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.

Comments

2

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.

1 Comment

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

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?

Comments

0

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

Comments

0

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

Comments

-1

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)*/)        

1 Comment

How does this relate to the question asked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.