C++关于动态数组实现的问题

xioacd99
  • 6

我尝试自己实现一个动态数组的模板类,运行程序的时候,程序出现了一个断点(我在代码中注释标明了),显示是delete []num那里,如果把这条语句注释掉,可以在dev-c编译器运行,但不能在vs运行。如果不注释,两者都不能运行。有人可以告诉我为什么吗,怎样解决呢?thanks

#include <iostream>

using namespace std;

#ifndef Dynamic_Num
#define Dynamic_Num

template <class T>
class DynamicVector{
    private:
        T *array = NULL; //类内初始值
        unsigned int mallocSize = 0, numOfItems = 0;
        int virtualZero = 0;

    public:
        template<class Y>
        friend ostream &operator<<(ostream &out, const DynamicVector<Y> &obj);

        DynamicVector() : array(NULL), mallocSize(0), numOfItems(0), virtualZero(0) {}
        DynamicVector(int index) : array(NULL), mallocSize(0), numOfItems(0), virtualZero(index) {}
        DynamicVector<T> operator()(int begin, int end);
        ~DynamicVector(){}
        DynamicVector(const DynamicVector &obj);

        unsigned int length() const { return numOfItems; }
        unsigned int capacity() const { return mallocSize; }
        int firstIndex() const { return virtualZero; }
        T &operator[](int index);
        
        bool operator==(const DynamicVector<T> &obj) const;

        void insert(int index, const T &obj);
        DynamicVector<T> &operator=(const DynamicVector<T> &obj);
        inline void push_back(const T &obj);
        void push_back(const DynamicVector<T> &obj);

        void swap(DynamicVector<T> &obj);
        void remove();
        void remove(int index);
        void remove(int begin, int end);
};

template<typename T>
DynamicVector<T>& DynamicVector<T>:: operator=(const DynamicVector<T> &obj){
    if(array&&numOfItems)
        delete[] array;
    array = new T[mallocSize];
    numOfItems = obj.numOfItems;
    mallocSize = obj.mallocSize;
    virtualZero = obj.virtualZero;
    for (int i = 0; i < numOfItems;++i){
        *(array + i) = *(obj.array + i);
    }
    return *this;
}

template <class T>
ostream &operator<<(ostream &out, const DynamicVector<T> &obj){
    if(obj.numOfItems){
        for (int i = 0; i < obj.numOfItems;++i){
            cout << *(obj.array + i) << " ";
        }
    }
    else{
        cout << "The arrray is empty.\n";
    }
    return out;
}

template <class T>
DynamicVector<T>::DynamicVector(const DynamicVector &obj){
    numOfItems = obj.numOfItems;
    mallocSize = obj.mallocSize;
    virtualZero = obj.virtualZero;
    array = new T[mallocSize];

    for (int i = 0; i < numOfItems;++i){
        *(array + i) = *(obj.array + i);
    }
}

template <class T>
T &DynamicVector<T>::operator[](int index){
    int entry = index - virtualZero;
    if(entry<0||entry>=numOfItems){
        cerr << "you have enter a invalid data.";
        exit(1);
    }
    return *(array + index - virtualZero);
}

template <class T>
bool DynamicVector<T>::operator==(const DynamicVector<T> &obj) const{
    if(numOfItems==obj.numOfItems&&virtualZero==obj.virtualZero){
        for (int i = 0; i < numOfItems;++i){
            if(*(array+i)!=*(obj.array+i))
                return false;
        }
    }
    else{
        return false;
    }
    return true;
}

template <class T>
DynamicVector<T> DynamicVector<T>::operator()(int begin, int end){
    DynamicVector<T> temp(virtualZero);
    for (int i = begin-virtualZero; i < end-virtualZero;++i){
        temp.push_back(array[i]);
    }
    return temp;
}

template <class T>
void DynamicVector<T>::insert(int index, const T &obj){
    T *temp = new T[numOfItems];
    for (int i = 0; i < numOfItems;++i)
        *(temp + i) = *(array + i);
    if (numOfItems + 1 > mallocSize){
        if(array&&numOfItems){
            delete[] array;
            array = NULL;
        }
        array = new T[2 * mallocSize];
    }
    for (int i = 0; i < index-virtualZero; ++i){
        *(array + i) = *(temp + i);
    }
    *(array + index - virtualZero) = obj;
    for (int i = index - virtualZero,j=i+1; j < numOfItems + 1;++i,++j){
        *(array + j) = *(temp + i);
    }
    delete[] temp;
    ++numOfItems;
}

template <class T>
inline void DynamicVector<T>::push_back(const T &obj){
    if(numOfItems<mallocSize){
        *(array + numOfItems) = obj;
    }
    else{
        T *temp = new T[numOfItems];
        for (int i = 0; i < numOfItems;++i)
            *(temp + i) = *(array + i);
        
        if(array&&numOfItems){
            delete[] array;
            array = NULL;
        }
        array = new T[2 * mallocSize+1];
        mallocSize = 2 * mallocSize + 1;
        for (int i = 0; i < numOfItems;++i){
            *(array + i) = *(temp + i);
        }
        array[numOfItems] = obj;
        delete[] temp;
        temp = NULL;
    }
    ++numOfItems;
}

template <class T>
void DynamicVector<T>::push_back(const DynamicVector<T> &obj){
    if(mallocSize<=numOfItems+obj.numOfItems){
        T *temp = new T[mallocSize];
        for (int i = 0; i < numOfItems;++i)
            *(temp + i) = *(array + i);
        if(array&&numOfItems){
            delete[] array;
            array = NULL;
            array = new T[mallocSize + obj.mallocSize];
        }
        for (int i = 0; i < numOfItems;++i)
            *(array + i) = *(temp + i);
        delete[] temp;
    }
    for (int i = numOfItems; i < numOfItems + obj.numOfItems;++i)
        *(array + i) = *(obj.array + i - numOfItems);
    numOfItems += obj.numOfItems;
}

template <class T>
void DynamicVector<T>::swap(DynamicVector<T> &obj){
    T *temp= array;array = obj.array;obj.array = temp;
    unsigned t1 = obj.numOfItems;obj.numOfItems = numOfItems;numOfItems = t1;
    unsigned t2 = obj.mallocSize;obj.mallocSize = mallocSize;mallocSize = t2;
    int v = virtualZero;virtualZero = obj.virtualZero;obj.virtualZero = v;
}

template <class T>
void DynamicVector<T>::remove(){
    --numOfItems;
}

template<class T>
void DynamicVector<T>:: remove(int index){
    T *num = new T[mallocSize];
    for (int i = 0,j=0; i < numOfItems;++i){
        if(i==index-virtualZero){
            continue;
        }
        *(num + j) = *(array + i);
        ++j;
    }
    for (int i = 0; i < numOfItems - 1;++i){
        *(array + i) = *(num + i);
    }
    //当我运行程序的时候,这里出现了一个断点
    delete[] num;
    --numOfItems;
}

template<class T>
void DynamicVector<T>:: remove(int begin, int end){
    T *num = new T[mallocSize];
    for (int i = 0,j=0; i < numOfItems;++i){
        if(i>=begin-virtualZero&&i<end-virtualZero){
            continue;
        }
        *(num + j) = *(array + i);
        ++j;
    }
    for (int i = 0; i < numOfItems - (end - begin-1);++i){
        *(array + i) = *(num + i);
    }
        numOfItems -= (end - begin-1);
    delete[] num;
}

#endif

int main()
{
    DynamicVector<int> ra(-2);
    

    int i,n;
    cin>>n;

    cout<<ra;

    ra.push_back(-3);
    ra.push_back(-2);
    ra.push_back(-1);
    for ( i = 0; i < n; i++){
        ra.push_back(i);
    }
    cout<<"\n malloSize is "<<ra.capacity();
    cout<<"\n numofItems is "<<ra.length();
    cout<<"\n StartIndex is " << ra.firstIndex()<<endl;
    for ( i = -2; i < n+1; i++){
        cout<<ra[i]<<" ";      
    }
    cout<<endl;

    DynamicVector<int> raCopy(ra);
    cout<<"\n malloSize is "<<raCopy.capacity();
    cout<<"\n numofItems is "<<raCopy.length();
    cout<<"\n StartIndex is " << raCopy.firstIndex()<<endl;
    cout<<endl;
    for ( i = -2; i < n+1; i++){   
        cout<<++ra[i]<<" ";      
    }
    cout<<endl;
    for ( i = -2; i < n+1; i++){   
        cout<<raCopy[i]<<" ";      
    }
    
    raCopy=ra;
    if (ra==raCopy)  
        cout<<"\n ra == raCopy";
    else 
        cout<<"\n ra != raCopy";
    

    ra[-2]=100;
    
    if (ra==raCopy)  
        cout<<"\n ra == raCopy";
    else 
        cout<<"\n ra != raCopy";
    
    raCopy.push_back(ra);
    cout<<endl;
    

    int firstI=raCopy.firstIndex();
    for ( i = 0; i < raCopy.length() ; i++){   
        cout<<raCopy[i+firstI ]<<" ";      
    }

    cout<<endl;
    raCopy.insert(-2,6);
    raCopy.insert(-1,7);
    cout<<raCopy;
    
    raCopy.remove();    
    cout<<endl;
    cout<<raCopy<<" remove()";


    raCopy.remove(-1);    
    cout<<endl;
    cout<<raCopy<<" remove(-1)";
    
    raCopy.remove(-1,1);    
    cout<<endl;
    cout<<raCopy<<" remove(-1,1)";

    ra=raCopy(-1,3);    
    cout<<endl;
    cout<<ra<<" raCopy(-1,3)";

    ra.swap(raCopy);    
    cout<<endl<<"ra.swap(raCopy)"<<endl;
    cout<<ra;
    cout<<endl;
    cout<<raCopy;

    
    system("pause");
    return 0;
}
回复
阅读 484
1 个回答
Hata
  • 2
新手上路,请多包涵

一般是由于你哪里内存没操作好的缘故,越界或者是操作了野指针。
不过删除元素不需要开辟新空间啊,原地移动不就行了吗,就像这样。

template<class T>
void DynamicVector<T>::remove(int index) {
    this->remove(index, index + 1);
}

template<class T>
void DynamicVector<T>::remove(int begin, int end) {
    std::memmove(array + (begin - virtualZero), array + (end - virtualZero), (numOfItems - (end - virtualZero)) * sizeof(T));
    numOfItems -= (end - begin);
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
你知道吗?

宣传栏