I wanted something similar, so I wrote this little tool just a heading that allows you to set (arr, pos, value) and get (arr, pos) and can enable or disable, confirm or allow continue and fail without checking.
https://github.com/goblinhack/c-plus-plus-array-bounds-checker
The essence of this is as follows (I have 2 and 3-dimensional examples on GitHub)
For debug builds:
#define DEBUG
#define ENABLE_ASSERT
#define ENABLE_ABORT
#include "array_bounds_check.h"
Some implementation details:
template<class TYPE, std::size_t XDIM> void set(std::array<TYPE,XDIM>& arr, std::size_t X, TYPE v){ DODEBUG(std::cerr << "set [" << X << "] = " << v << std::endl); ASSERT(X >= 0) ASSERT(X < arr.size()) arr[X] = v; } template<class TYPE, std::size_t XDIM> TYPE& get(std::array<TYPE,XDIM> & arr, std::size_t X){ DODEBUG(std::cerr << "get [" << X << "] = "); ASSERT(X >= 0) ASSERT(X < arr.size()) DODEBUG(std::cerr << arr[X] << std::endl); return (arr[X]); }
If you want to enable tracing of the calls to set () and get (), enable:
#define DEBUG
To print the statement out of bounds (and continue):
#define ENABLE_ASSERT
To call abort () to confirm:
#undef ENABLE_ABORT
NTN
Neil mcgill
source share