Just Too Long...

I'm pretty sure my matrix vector multiply method is having problems with the vector size I need it to handle. I tried switching to "new" in order to put the y vector on the heap but I still get a segmentation fault. It works fine for size 1806, but crashes for 1922.

My main() has several of similar sized vectors as well as Matrix A, but A is sitting on the heap (or should be).

I'm on cygwin now so I can't change the stack size (at least thats what I've read...and I only have 2400 bytes or so). I will attempt to do so on my office linux box, but It would be nice if this would work with a normal stack size.

My template, T, is long double most of the time.

Here's the code for MV-mult (the output is correct):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::vector<T> operator*(const std::vector<T>& x)
{  //Computes A*x, returns in vector y

    std::vector<T> y(this->m);
    T sum;
    row_iter ii;
    col_iter jj;

    for(ii=this->mat.begin(); ii!=this->mat.end(); ii++){
        sum=0;
        for(jj=(*ii).second.begin(); jj!=(*ii).second.end(); jj++){
            sum += (*jj).second * x[(*jj).first];
        }
        y[(*ii).first]=sum;
    }

    return y;
}


I've also tried double instead of long double, it still has a segmentation error.
Last edited on
std::vectors create their arrays in the heap. If you're getting a segmentation fault, it's from your own code.

You're using Linux, right? Try running your code through Valgrind.
(1) you should preferrably use ++i i and ++jj instead of ii++ or jj++, but that is probably not the main problem

(2) the segmentation fault will come from an out-of-range index of x or y. This piece of code simply assumes that the numbers (*jj).first and (*ii).first are within the proper range 0..(this->m -1).
This is asking for trouble - add a test, and see where it goes wrong...




What is this->m set to? The vector is not going to grow when you use operator[] to access elements. If the vector needs to grow dynamically then you need to use the insert member function and not operator[]. I do not recommend performing complex calculations within the operator[] because this operator does not throw exceptions. It'll just crash if you use an invalid index. Another way of debugging is to use the at member function instead of operator[] which does throw std::out_of_range which you can catch.

Clearly you are running into a problem where you are trying to access invalid elements in the vector. I think you should be able to debug this fairly easily. Perform the index calculation first like so:

1
2
3
4
5
6
7
8
9
10
11
std::vector<T>::size_type index(0);
index = (*ii).first;
try
{
   y.at(index) =sum;
}
catch(std::out_of_range&)
{
   std::cout << "out of range caught using index = " << index << std::endl;
   std::cout << "vector instance y has size = " << y.size() << std::endl;
}


Do something similar in the for loop with your x vector. The value of index should never exceed what size returns for the vector.
Topic archived. No new replies allowed.