one of my vectors was acessed out of bounds inside a function
like
void foo(
std::vector<int>& arr
)
{
int i=0;
while(...)
{
//... do useful stuff with i
arr[i]++;
//... do useful stuff
}
int main()Quote:}
{
std::vector<int> a(100);
foo(a);
and I wanted to trace where the error was. For this I wrote the followingQuote:}
class,
template<class arrT, class elmT>
class DebugArrayWraper
{
arrT& arr;
int maxind;
public:
DebugArrayWraper(arrT& a, int size):arr(a), maxind(size-1){}
elmT& operator[](int index)
{
_ASSERT(index<=maxind);
return arr[index];
the idea being that you can pass to it either a vector or an array (elmT*);Quote:}
};
and an assert will be triggered at the point of violation. To simplify it I
also wrote a macro
#define DECLARE_ARRAY_WRAPPER(Type, elmType)\ DebugArrayWraper<Type,
elmType>
so that the usage is
void foo(
DECLARE_ARRAY_WRAPPER(std::vector<int>, int)& arr
)
{
...
int main()Quote:}
{
DECLARE_ARRAY_WRAPPER(std::vector<int>, int) a(100);
foo(a);
This works nicely (I traced the bug), but I could not think of a way to getQuote:}
rid of
the annoying second parameter (at least to the macro). Any suggestions?
max.
[ about comp.lang.c++.moderated. First time posters: do this! ]