将新元素添加到动态数组c ++中

时间:2021-01-23 15:54:55

I know those are basics, but I can't get over with it. I want to add x times y elements to my array. When I add 1x100000 elements it's fine. However, when I try to add 5000x5000 elements, it stops immediately, with many different errors, never bad alloc. I'd be extremaly gladful if someone can give me a clue about this...

我知道那些是基础知识,但我无法克服它。我想将x次y元素添加到我的数组中。当我添加1x100000元素时,它很好。但是,当我尝试添加5000x5000元素时,它立即停止,有许多不同的错误,从不错误的alloc。如果有人能给我一个关于这一点的线索,我会非常高兴...

#include <iostream>
#include <numeric>
#include <chrono>
#include <cstdlib>

class Array
{
    int *tab; //dynamic array
    int cnt; //count
public:
    Array();
    void clearArray();//deleting table
    void test_addValueToArray(int index, int value);
};

Array::Array() : tab(nullptr), cnt(0){;}

void Array::clearArray()
{
    if(cnt==0)
        return;
    delete tab;
    cnt=0;
}

void Array::test_addValueToArray(int index, int value)
{
    int *NewTab = new int[cnt+1];
    for(int i=0;i<index;++i)
        NewTab[i]=tab[i];
    NewTab[index]=value;
    for(int i=index+1;i<cnt+1;++i)
        NewTab[i]=tab[i-1];
    delete[] tab;
    tab=NewTab;
    ++cnt;
}

Array myArray;

int main()
{
    int elements, times;
    std::cout<<"How many elements?";
    std::cin>>elements;
    std::cout<<"How many times?";
    std::cin>>times;
    auto start = std::chrono::high_resolution_clock::now();
    for(int j=0; j<times; ++j)
    {
        for(int i=0; i<elements ; ++i)
            myArray.test_addValueToArray(0,rand()%1000);
        myArray.clearArray();
    }
    auto stop = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> time=stop-start;
    std::cout<<"Avarage time:"<<time.count()/times<<"\n";
}

1 个解决方案

#1


0  

Problem is because of use of delete[] for lots of continious memory deallocation, plus you have used only delete tab which should be changed to delete[] tab in cleararray so you can use smart pointers.

问题是因为使用delete []进行大量连续的内存释放,而且你只使用了删除选项卡,应该更改为删除cleararray中的[]选项卡,这样你就可以使用智能指针。

plus: In your test_addValueToArray() when cnt=0 you are trying to deallocate memory which is not allocated at all and i didn't understand why you are passing 0 as index while calling addValueToArray() i think it should be i at that point.

加:在你的test_addValueToArray()当你的cnt = 0时,你试图释放根本没有分配的内存,我不明白为什么你在调用addValueToArray()的同时传递0作为索引我想我应该在那时。

So once you fix this and use smart pointers it should work else it will show bad_alloc exception after running for some time.

因此,一旦你修复了这个并使用智能指针,它应该工作,否则它会在运行一段时间后显示bad_alloc异常。

#1


0  

Problem is because of use of delete[] for lots of continious memory deallocation, plus you have used only delete tab which should be changed to delete[] tab in cleararray so you can use smart pointers.

问题是因为使用delete []进行大量连续的内存释放,而且你只使用了删除选项卡,应该更改为删除cleararray中的[]选项卡,这样你就可以使用智能指针。

plus: In your test_addValueToArray() when cnt=0 you are trying to deallocate memory which is not allocated at all and i didn't understand why you are passing 0 as index while calling addValueToArray() i think it should be i at that point.

加:在你的test_addValueToArray()当你的cnt = 0时,你试图释放根本没有分配的内存,我不明白为什么你在调用addValueToArray()的同时传递0作为索引我想我应该在那时。

So once you fix this and use smart pointers it should work else it will show bad_alloc exception after running for some time.

因此,一旦你修复了这个并使用智能指针,它应该工作,否则它会在运行一段时间后显示bad_alloc异常。