【问题标题】:allocating extra memory for a container class为容器类分配额外的内存
【发布时间】:2010-05-15 13:04:53
【问题描述】:

嘿,我正在编写一个模板容器类,在过去的几个小时里一直在尝试为进入容器的额外数据分配新内存(......撞到砖墙......:|)

template <typename T>
void Container<T>::insert(T item, int index){
    if ( index < 0){
        cout<<"Invalid location to insert " << index << endl;
        return;
    }


    if (index < sizeC){ 
    //copying original array so that when an item is 
    //placed in the middleeverything else is shifted forward
        T *arryCpy = 0;
        int tmpSize = 0;
        tmpSize = size();
        arryCpy = new T[tmpSize];
        int i = 0, j = 0;
        for ( i = 0; i < tmpSize; i++){
            for ( j = index; j < tmpSize; j++){
                arryCpy[i] = elements[j];
            }
        }
        //overwriting and placing item and location index
        elements[index] = item;
        //copying back everything else after the location at index
        int k = 0, l = 0;
        for ( k =(index+1), l=0; k < sizeC || l < (sizeC-index); k++,l++){
            elements[k] = arryCpy[l];
        }
        delete[] arryCpy;
        arryCpy = 0;
    }

    //seeing if the location is more than the current capacity
    //and hence allocating more memory
    if (index+1 > capacityC){
        int new_capacity = 0;
        int current_size = size();
        new_capacity = ((index+1)-capacityC)+capacityC;
        //variable for new capacity

        T *tmparry2 = 0;
        tmparry2 = new T[new_capacity];
        int n = 0;
        for (n = 0; n < current_size;n++){
            tmparry2[n] = elements[n];
        }
        delete[] elements;
        elements = 0;
        //copying back what we had before
        elements = new T[new_capacity];
        int m = 0;
        for (m = 0; m < current_size; m++){
            elements[m] = tmparry2[m];
        }
            //placing item
        elements[index] = item;
    }

    else{
        elements[index] = item;
    }
    //increasing the current count
    sizeC++;

我的测试条件是 容器cnt4(3); 一旦我击中第四个元素(当我使用 egsomething.insert("random",3); 时)它就会崩溃并且上述方法不起作用。我哪里出错了?

【问题讨论】:

    标签: c++ templates memory-management new-operator containers


    【解决方案1】:

    有几件事对我来说没有太大意义:

    if (index+1 > capacityC){
    

    不应该这样吗:

    if (index >= capacityC){
    

    此外,当您扩大阵列时,我不明白您为什么要进行两次大量复制。不应该:

    delete[] elements;
    elements = 0;
    

    是:

    delete[] elements;
    elements = tmparray2;
    

    【讨论】:

      【解决方案2】:

      注意new T[n] 可能不是您在T 容器中真正想要的,因为这已经创建了n 个T 类型的对象。您真正想要的是保留内存,然后在稍后的某个时间点在该内存中构造T 类型的对象。

      T* data = static_cast<T*>(operator new[](n * sizeof(T));   // allocate memory
      // ...
      new(&data[size]) T(arguments);   // construct T object in-place
      ++size;
      

      要想销毁容器,就得逆过程:一一销毁对象,然后释放内存。

      while (size) data[--size].~T();   // destruct T object in-place
      operator delete[](data);          // release memory
      

      【讨论】:

        猜你喜欢
        • 2014-12-17
        • 2011-07-11
        • 1970-01-01
        • 2012-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        相关资源
        最近更新 更多