为容器类分配额外的内存

发布于 2024-09-01 15:13:50 字数 2103 浏览 8 评论 0原文

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

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); 一旦我点击第四个元素(当我使用 for 例如something.insert("random",3);)时,它就会崩溃并且上面的方法不起作用。我哪里出错了?

Hey there, I'm writing a template container class and for the past few hours have been trying to allocate new memory for extra data that comes into the container (...hit a brick wall..:| )

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++;

my testing condition is
Container cnt4(3);
and as soon as i hit the fourth element (when I use for egsomething.insert("random",3);) it crashes and the above doesnt work. where have I gone wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-09-08 15:13:50

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

if (index+1 > capacityC){

不应该是这样吗:

if (index >= capacityC){

另外,当你增加数组时,我不明白你为什么要进行两次复制。不应该:

delete[] elements;
elements = 0;

是:

delete[] elements;
elements = tmparray2;

Several things don't make too much sense to me:

if (index+1 > capacityC){

shouldn't that be:

if (index >= capacityC){

Also, when you grow the array I don't see why you are doing two lots of copying. shouldn't:

delete[] elements;
elements = 0;

be:

delete[] elements;
elements = tmparray2;
記憶穿過時間隧道 2024-09-08 15:13:50

请注意,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

Note that new T[n] is probably not what you actually want in a container of T, because this already creates n objects of type T. What you really want is to reserve memory, and then at some later point in time construct objects of type T in that memory.

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

In order to destruct the container, you have to reverse the process: destruct the objects one by one and then release the memory.

while (size) data[--size].~T();   // destruct T object in-place
operator delete[](data);          // release memory
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文