【问题标题】:C++ Double free or corruption Error when I believe I'm only freeing once当我相信我只释放一次时,C++ 双重释放或损坏错误
【发布时间】:2015-02-06 21:39:15
【问题描述】:

我的代码中的最后一个函数破坏了数组,我遇到了问题,我不断得到 Double free 或损坏,我认为这意味着我要释放它两次,但我不知道我是怎么回事这样做

主代码

#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& arr)

谢谢。

【问题讨论】:

  • 您的问题可能是您认为您只释放了一次。相信你没有做错任何事会让你发现自己做错了什么。
  • 使用std::unique_ptrstd::shared_ptr 就不用担心了。

标签: c++


【解决方案1】:
arr.data = new_data;
delete [] arr.data;

应该是

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多