Linear Algebra Module User’s Guide : Chapter 3 Vectors and Matrices : Accessing Data : Bounds Checking
Bounds Checking
Bounds checking is done using operator()() only if the preprocessor symbol RWBOUNDS_CHECK is defined when the matrix header file is read in. Usually, this is done via a compiler flag, but for examples here we use explicit preprocessor directives. In the following code, the symbol RWBOUNDS_CHECK is defined, so it causes a runtime error.
 
#define RWBOUNDS_CHECK // Do bounds checking
#include <rw/lapack/trdgmat.h>
 
int main()
{
// Define a 3x3 tridiagonal matrix and try to access
// element (3,2), which does not exist!
RWTriDiagMat<DComplex> A(3, 3);
DComplex el = A(3,2); // Fails here!!!
}
On the other hand, the out-of-bounds reference in this code:
 
#undef RWBOUNDS_CHECK // No bounds checking
#include <rw/lapack/trdgmat.h>
 
int main()
{
// Define a 3x3 tridiagonal matrix and try to read
// element (3,2), which does not really exist
RWTriDiagMat<DComplex> A(3,3);
DComplex el = A(3,2); // No bounds check!!!
}
is undetected unless it causes a segmentation error. This is because bounds checking was not enabled. The results of examining an out-of-bounds element are undefined and likely to return garbage. The results of setting an out-of-bounds element are also undefined, but likely to bring disaster! It is an excellent idea to use bounds checking except when execution speed is very important.