Commit 60c1d035 authored by Martin Nolte's avatar Martin Nolte
Browse files

also fix pointer arithmetic in the copy constructor

The problem with the old implementation was this:
diff = (coeff_ - other.coeff_) is a difference between pointers of type Field *.
Therefore, this difference includes an integer division (by sizeof( Field )).
If the pointers were not aligned on sizeof( Field ) boundary, rounding occurs.
Next thing other.rows_[ i ] + diff results in remultiplying with this size, bout
the roundoff error persists and might, hence, lead to a segmentation fault.

The solution was easy: use (other.rows_[ i ] - other.coeff_) for the difference.
Since other.rows_[ i ] = k * sizeof( Field ) + other.coeff_ for some k, this division
is safe.

[[Imported from SVN: r873]]
parent e94551d8
......@@ -206,10 +206,7 @@ namespace Dune
skip_[i] = skip[i];
}
for (unsigned int i=0; i<=numRows_; ++i)
{
ptrdiff_t offset = ( rows_[i] - coeff);
rows_[i] = coeff_ + offset;
}
rows_[ i ] = coeff_ + (rows_[ i ] - coeff);
delete [] coeff;
delete [] skip;
......@@ -236,7 +233,7 @@ namespace Dune
}
}
private:
SparseCoeffMatrix(const This &other)
SparseCoeffMatrix ( const This &other )
: numRows_( other.numRows_ ),
numCols_( other.numCols_ )
{
......@@ -250,8 +247,9 @@ namespace Dune
skip_[i] = other.skip_[i];
}
for (unsigned int i=0; i<=numRows_; ++i)
rows_[i] = other.rows_[i]+(coeff_-other.coeff_);
rows_[ i ] = coeff_ + (other.rows_[ i ] - other.coeff_);
}
This &operator= (const This&);
Field *coeff_;
Field **rows_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment