C ++ Double free或corruption当我相信我只释放一次时出错

时间:2015-02-06 21:39:16

标签: c++

我的代码中的最后一个函数破坏了数组的问题,我不断获得双重自由或腐败,我认为这意味着我将它释放两次,但我无法弄清楚我是怎么回事这样做

主要代码

#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;
}

功能代码

#include "terrible_dynamic_size_array_unsorted.h"

using namespace std;


void init(int_array& arr)
{
    arr.count = 0; //set count to 0
    arr.capacity = arr.DEFAULT_CAPACITY;
    arr.data = new int[arr.capacity];
}

void clear(int_array& arr)
{
    destr(arr); //destroys array
    init(arr); // initializes array
}

void destr(int_array& arr) //function for destroying array
{
    delete[] arr.data;
    //*arr.data = 0;
    arr.count = 0;
}

void print(const int_array& arr) //prints out the array
{
    for (unsigned int i = 0; i < arr.count; ++i)
        cout << arr.data[i] << " ";
    cout << endl;
}

bool contains(const int_array& arr, const int& target) //
{
    unsigned int i;

    for (i = 0; i < arr.count; ++i)
    {
        if (arr.data[i] == target) return true;
        //else return false;
    }
    return false;
}

void resize(int_array& arr) //resizes the array --- WORKING
{
    arr.capacity *= 2;
    int* new_data = new int[arr.capacity];
    for (unsigned int i = 0; i < arr.count; ++i)
    {
        new_data[i] = arr.data[i];
    }

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

}

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

    if ((arr.count == arr.capacity))
        resize(arr);


    arr.data[++arr.count] = payload;

}

bool remove(int_array& arr, const int& target)
{
    unsigned int i = 0;


    if (arr.count == 0)
    {
        return false;
    }

    while (i <= arr.count && arr.data[i] != target)  {i++;}


    if (i > arr.count)
    {
        return false;
    }

    arr.data[i] = arr.data[arr.count];

    arr.count--;
    return true;
}

标头文件

#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);

问题在于函数void destr(int_array&amp; arr)

谢谢。

1 个答案:

答案 0 :(得分:0)

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

应该是

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

或者,旧数组将被泄露,arr.data将指向已释放的内存 - 然后destr将再次非法释放内存。