C++ C++;当我相信我';我只有一次自由

C++ C++;当我相信我';我只有一次自由,c++,C++,我的代码中最后一个破坏数组的函数出现了问题,我不断得到双重释放或损坏,我想这意味着我要释放它两次,但我不知道我是如何做到的 主代码 #include "terrible_dynamic_size_array_unsorted.h" using namespace std; int main() { int_array arraytest; init(arraytest); if(arraytest.count==0) { cout<&l

我的代码中最后一个破坏数组的函数出现了问题,我不断得到双重释放或损坏,我想这意味着我要释放它两次,但我不知道我是如何做到的

主代码

#include "terrible_dynamic_size_array_unsorted.h"

using namespace std;


int main()
{
    int_array arraytest;
    init(arraytest);
    if(arraytest.count==0)
    {
        cout<<"Empty array created"<<endl;
    }
    else
    {
        cout<<"Error in array creation"<<endl;
    }
    clear(arraytest);
    if(arraytest.count==0)
    {
        cout<<"Array cleared of data"<<endl;
    }
    else
    {
        cout<<"Error in clear function"<<endl;
    }
    for(unsigned int i=0;i<25;i+=2)
    {
        if(arraytest.count < arraytest.DEFAULT_CAPACITY)
        {
            add(arraytest,i);
            print(arraytest);
        }
        else
        {
            add(arraytest,i);
            print(arraytest);
        }
    }
    for(unsigned int i=1;i<25;i+=2)
    {
        if(arraytest.count < arraytest.DEFAULT_CAPACITY)
        {
            add(arraytest,i);
            print(arraytest);
        }
        else
        {
            add(arraytest,i);
            print(arraytest);
        }
    }
    if(arraytest.capacity == 2*arraytest.DEFAULT_CAPACITY)
    {
        cout<<"Resize function works properly"<<endl;
    }
    else
    {
        cout<<"Resize not working properly"<<endl;
    }
    if(contains(arraytest,6))
    {
        cout<<"Number 6 present in Array"<<endl;
    }
    else
    {
        cout<<"Number 6 not in Array Contains not working properly"<<endl;
    }
    if(contains(arraytest,30))
    {
        cout<<"Number 30 present in Array Contains not working properly"<<endl;
    }
    else
    {
        cout<<"Number 30 not in Array"<<endl;
    }
    if(remove(arraytest,23) && arraytest.count == 24)
    {
        cout<<"Number 23 removed from Array"<<endl;
    }
    else
    {
        cout << arraytest.count << endl;
        cout<<"Number 23 not in Array error in remove"<<endl;
    }
    if(remove(arraytest,24) && arraytest.count == 23)
    {
        cout<<"Number 24 removed from Array"<<endl;
    }
    else
    {
        cout<<"Number 24 not in Array error in remove"<<endl;
    }
    if(remove(arraytest,0) && arraytest.count == 22)
    {
        cout<<"Number 0 removed from Array"<<endl;
    }
    else
    {
        cout<<"Number 0 not in Array error in remove"<<endl;
    }
    if(remove(arraytest,35))
    {
        cout<<"Error in remove function"<<endl;
    }
    else
    {
        cout<<"Number not in Array"<<endl;
    }
    destr(arraytest);
    if(*arraytest.data == 0)
    {
        cout<<"Array destroyed"<<endl;
    }
    else
    {
        cout<<"Error in destroy"<<endl;
    }
    return 0;
}
#包括“糟糕的动态大小数组未排序.h”
使用名称空间std;
int main()
{
int_阵列测试;
init(arraytest);
如果(arraytest.count==0)
{
库特
应该是

delete[] arr.data;
arr.data = new_data;

或者,旧数组将泄漏,并且
arr.data
将指向已释放的内存-然后将通过
destr

再次非法释放该内存。您的问题可能是您认为您只释放了一次。相信您没有做错任何事情将阻止您查找错误。请使用
std::unique_ptr
std::shared_ptr
您不必担心。
#include <iostream>

struct int_array {
    int* data;
    unsigned int count;
    unsigned int capacity;
    static const unsigned int DEFAULT_CAPACITY = 20;
};

void init(int_array& arr);

void destr(int_array& arr);

void resize(int_array& arr);

void clear(int_array& arr);

void add(int_array& arr, const int& payload);

bool contains(const int_array& arr, const int& target);

bool remove(int_array& arr, const int& target);

void print(const int_array& arr);
arr.data = new_data;
delete [] arr.data;
delete[] arr.data;
arr.data = new_data;