Debug array wrapper class

Debug array wrapper class

Post by maxi » Fri, 12 Nov 1999 04:00:00



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
    }

Quote:}

int main()
{
    std::vector<int> a(100);
    foo(a);

Quote:}

and I wanted to trace where the error was. For this I wrote the following
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];

Quote:}
};

the idea being that you can pass to it either a vector or an array (elmT*);
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
                )
{
    ...

Quote:}

int main()
{
    DECLARE_ARRAY_WRAPPER(std::vector<int>, int) a(100);
    foo(a);

Quote:}

This works nicely (I traced the bug), but I could not think of a way to get
rid of
the annoying second parameter (at least to the macro). Any suggestions?
max.


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Debug array wrapper class

Post by Darin Adle » Sat, 13 Nov 1999 04:00:00



> and I wanted to trace where the error was. For this I wrote the following
> 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];
>     }
> };

> int main()
> {
> 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 get
> rid of the annoying second parameter (at least to the macro). Any suggestions?

Use the array helpers defined in the boost.org header array.hpp. Keeping the
style close to your original, here is a cut at it:

    #include <array.hpp>
    #include <iterator>

    template<class arrT>
    class DebugArrayWrapper
    {
        arrT& arr;
        typedef boost::array_traits<arrT>::iterator itrT;
        typedef std::iterator_traits<itrT>::value_type elmT;
    public:
        DebugArrayWrapper(arrT& a) : arr(a) { }
        elmT& operator[](int index)
        {
            _ASSERT(index >= 0);
            _ASSERT(index < boost::size(arrT));
            return arr[index];
        }
    };

To get array.hpp, go to www.boost.org and look for the array library.

    -- Darin


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Debug array wrapper class

Post by Steve Foll » Sat, 13 Nov 1999 04:00:00



Quote:> #define DECLARE_ARRAY_WRAPPER(Type, elmType)\ DebugArrayWraper<Type,
> elmType>

> This works nicely (I traced the bug), but I could not think of a way to
get
> rid of
> the annoying second parameter (at least to the macro). Any suggestions?
> max.

If you're concerned about writing 'int' twice, what about redefining it
thus:

#define DECLARE_ARRAY_WRAPPER(Type, elmType) \
    DebugArrayWrapper<Type < elmType >, elmType>

And changing the declaration:

int main()
{
    DECLARE_ARRAY_WRAPPER(std::vector, int) a(100);
    foo(a);

Quote:}

An idea, probably one of many. Hope it helps.

--
Regards,
Steve Folly.
http://www.follysplace.demon.co.uk

"Science is, foremost, a method of interrogating reality: proposing
hypotheses that seem true and then testing them -- trying, almost
perversely, to negate them, elevating only the handful that survive to
the status of a theory. Creationism is a doctrine, whose adherents are
interested only in seeking out data that support it."
        George Johnson, NY Times, 15 Aug 1999


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Debug array wrapper class

Post by Andrei Alexandresc » Mon, 15 Nov 1999 04:00:00



Quote:> If you're concerned about writing 'int' twice, what about redefining
it
> thus:

> #define DECLARE_ARRAY_WRAPPER(Type, elmType) \
>     DebugArrayWrapper<Type < elmType >, elmType>

> And changing the declaration:

> int main()
> {
>     DECLARE_ARRAY_WRAPPER(std::vector, int) a(100);
>     foo(a);
> }

I guess he doesn't want the macro to have two parameters. If I'm
right, this would cut the deal:

template<class arrT>
class DebugArrayWraper
{
    typedef typename arrT::value_typpe elmT; // here's the element
type
    ...

Quote:};

#define DECLARE_ARRAY_WRAPPER(Type)\
    DebugArrayWraper<Type/**/>

Usage would be:

DECLARE_ARRAY_WRAPPER(vector<int>) a(100);

Quote:> An idea, probably one of many. Hope it helps.

Idem :o).

Quote:> "Science is, foremost, a method of interrogating reality: proposing
> hypotheses that seem true and then testing them -- trying, almost
> perversely, to negate them, elevating only the handful that survive
to
> the status of a theory. Creationism is a doctrine, whose adherents
are
> interested only in seeking out data that support it."
>         George Johnson, NY Times, 15 Aug 1999

That's very cool C++.  I guess a couple of people wouldn't like it,
though. Was this rather off-topic trailer that necessary?

Andrei


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

1. debug array wrapper class

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()
{
    std::vector<int> a(100);
    foo(a);

and I wanted to trace where the error was. For this I wrote the following
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*);
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()
{
    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 get
rid of
the annoying second parameter (at least to the macro). Any suggestions?
max.

2. Access 2000 Install on WTS

3. Debug array wrapper class

4. V3R1M3 CA/400 Printing problems.

5. wrapper for pointer array

6. More info....

7. Help: How to define a container class array inside another array?

8. Communicator/2 and User Profiles

9. Can a method of an element of an array of class objects know its array index?

10. initializing structure arrays/class arrays

11. How to create COM wrapper for C++ class?

12. bstr_t wrapper class problem?

13. Exist a wrapper class C++ for TAPI