gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Need help debugging code.

#7054 - MumblyJoe - Sat Jun 07, 2003 6:26 am

Hey, I have written a std::vector style class for arrays in my gba projects and it seems to work ok, but I would like it tested for robustness in some real world applications. If anyone has any projects that use arrays and add and remove items from them, could you give this a shot and tell me how it goes.

Basically it works just like an array, even uses operator[] and dynamically grows by 10 items when it gets full, but does not shrink as items are removed (I thought it would fragment memory too much on the GBA, would rather just be using it all up).

Any help would be great, and if you find it usefull feel free to steal it or modify it for your needs. It has really only been written because std::vector is to big to think about using and also it uses pure virtual functions, which of course break DKA under most circumstances.

PS: I know a linked list would be more dynamic but I feel the pointers involved outweigh the benifits on the GBA.


Code:
template<class T>
class Array
{
   public:
   Array(){array = new T[10]; size=10; top=0;}
   Array(const Array& rhs)
   {
      array = new T[10]; size=10; top=0;
      for(int i=0;i<rhs.top;i++)
         Add(rhs.array[i]);
   }
   ~Array(){delete [] array;}
   T& operator[](const int i){return array[i];}
   const Array& operator=(const Array& rhs)
   {
      delete [] array; array = new T[10]; size=10; top=0;
      for(int i=0;i<rhs.top;i++)
         Add(rhs.array[i]);
      return *this;
   }
   void Add(T wanted)
   {
      if(top==size)
         {
            T* temp = new T[size+10];
            for(int i=0;i<size;i++)
               temp[i] = array[i];
            delete [] array;
            array = temp;
            size+=10;
         }
      array[top++]=wanted;
   }
   void Remove(int i)
   {
      for(int j=i;j<(top-1);j++)
         array[j] = array[j+1];
      top--;
   }
   const int Size(){return top;}

   private:
   T* array;
   int size;
   int top;
};

_________________
www.hungrydeveloper.com
Version 2.0 now up - guaranteed at least 100% more pleasing!

#7056 - Sweex - Sat Jun 07, 2003 10:58 am

I've been working on a string and vector class too lately, and what I did for testing is use visual C++. This way you can more easily write a test program that does some (or a lot) of random Add and Remove operations, and it is a lot easier to debug it when it goes wrong.

There shouldn't be any reason why it would go wrong on GBA if it's fine in visual c++ (or in fact, any c++ compiler will do)

(I've coded my container classes cross platform, and I can set the allocation/deallocation mode with some #defines, depending on the platform)