Segmentation fault

Hey guys.
Yet again, I have this type of problem, I was wondering if anyone could give me a hand with it?
The problem lies on line 69, "sum += matrix[i][j] * y[i];", more specifically at "matrix[i][j]". This is basically a function that retrieves the matrix and the size of the matrix from a text file, then applies the Jacobi iterative method.
Everything seems to work fine, with the exception of the app crashing whenever it hits that part.
Here's the code (the part that matters):
code removed. will add it back once I deliver the assigment

Any idea what the problem could be?
Matrix has the size[n,n] so this makes absolutely no sense - to me that is.
Last edited on
Have you actually checked for sure that at that point in the code the vector actually contains n vectors of n elements each?

You should also be iterating based on what the vectors tell you their length is, not based on what you think or want their length to be.
Last edited on
I hadn't until now, but I tried adding:
cout << "lines:" << matrix.size() << "; columns: " << matrix[0].size() << endl;
After:
matrix[atoi(tmp[1].c_str())-1][atoi(tmp[2].c_str())-1] = strtod(tmp[3].c_str(), NULL);

And it specifies the correct size (726).
K, next question: how can you be sure that it is the matrix[i][j] part that is crashing and not the y[i] part?
nmax is never set to anything besides 0 in the code you show, so we're missing some relevant code. The size of vector x is nmax, so if nmax is less than n then you will be accessing out of range in vectors x and y in that loop.

There is also the possibility that you exceed the bounds of tmp in the while (isstream) loop, since you make no effort to ensure it stays in bounds. You also don't check, in the same code, to make sure there were no less than 3 istringstream extractions. There is also no attempt to make sure the bounds for vector matrix are observed at line 29.

Line 14 assumes the input extraction on line 11 was correct, but you don't check to see if it was.
Last edited on
I tried removing the y[i] part and the error persisted. If however I removed the matrix part and only kept the y, the error was gone.
@cire - As stupid as it sounds, I actually figured out the issue whilst reading your comment. Don't worry, nmax and epsilon are user inputted values and they are never 0. Problem was a missspecification, I actually checked everything but that,jeez.
What are the values of i and j at the time of the crash?
Topic archived. No new replies allowed.