Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   dynamic array problem (http://www.programmingforums.org/showthread.php?t=13152)

rwm May 14th, 2007 4:06 AM

dynamic array problem
 
Hey guys!

I'm trying to write a dynamic array. I was thinking of using the std STL library, but then I thought that if I write this then i'll understand better...

well, i honestly can't understand why it doesnt work:

:

#pragma once

typedef unsigned int uint;

template<typename T>
class DynamicArray {
        public:
                //constructors
                DynamicArray() {
                        size = 0;
                        data = 0;
                }
                DynamicArray(const DynamicArray &src) {
                        if(src.data) {
                                size = src.size;
                                data = new T[size];
                                //copy data
                                T *p = &src.data[0];
                                T *q = &data[0];
                                while(*p)
                                        *q++ = *p++;
                        } else {
                                size = 0;
                                data = 0;
                        }
                }
                //destructors
                ~DynamicArray() {
                        delete[] data;
                }
                ///...
                //methods
                void Append(const T &t) { //append item to end
                        if(data) {
                                T *tmp = &data[0];
                                size++;
                                data = new T[size];
                                //copy old data
                                T *p = &tmp[0];
                                T *q = &data[0];
                                while(*p)
                                        *q++ = *p++;
                                *q = t;
                                //free old mem
                                delete[] tmp; /// causing problems
                        } else {
                                size = 1;
                                data = new T(t);
                        }
                }
                void Remove() { //remove item at end
                        if(data) {
                                T *tmp = &data[0];
                                size--;
                                data = new T[size];
                                //copy old data
                                T *p = &tmp[0];
                                T *q = &data[0];
                                while(*q)
                                        *q++ = *p++;
                                //free old mem
                                delete[] tmp;
                        } else
                                throw int(99); ///throw Exception("array is empty",__FILE__,__LINE__);
                }
                //data
        private:
                //data
                uint size;
                T *data;
        protected:
                //data
}; //class DynamicArray


:

#include <iostream>
using namespace std;

#include "DynamicArray.h"

int main() {
        DynamicArray<float> arr;
        int i;
        for(i=0; i<50; i++) {
                cout << "appending " << i << endl;
                arr.Append((float)i);
        }

        return 0;
}


it crashes about halfway. the allocation seems very inconsistent :confused:

hope you can help me out! many thanks! ;)

grumpy May 14th, 2007 5:17 AM

The main problem I picked up at first glance is in your loops of the form;
:

  while (*p)
      *q++ = *p++;

which relies on your arrays being terminated with an element of value zero. However, you do not ensure your arrays are terminated that way.

There may be other problems as well; I stopped after encountering only one problem.

rwm May 14th, 2007 5:55 AM

ah of course! pretty obvious!

thanks for the pointer Grumpy!

i will fix this now - bit busy at work at the moment, but when i get a chance ill fix and get back! :banana:

Game_Ender May 14th, 2007 7:20 AM

Hint: use something like memcpy.

rwm May 14th, 2007 11:22 AM

hey,

well i finally got a chance to work on this, its all sorted now!

wow, such a stupid thing to do!

thanks for the help guys! much appreciated! :D


All times are GMT -5. The time now is 2:07 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC