2015-02-06 51 views
0

我遇到了我的代碼中的最後一個功能的問題,破壞數組,我不斷得到雙免費或腐敗,我認爲我的意思是釋放它兩次,但我無法弄清楚如何我這樣做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 & ARR)

謝謝。

+1

你的問題可能是你相信你只能釋放一次東西。相信你沒有做錯什麼會阻止你發現你做錯了什麼。 – gnasher729 2015-02-06 21:43:47

+1

使用'std :: unique_ptr'或'std :: shared_ptr',你不必擔心它。 – 2015-02-06 21:47:17

回答

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

應該

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

或者,舊陣列將被泄露和arr.data將指向已經釋放的內存 - 然後將這個非法再次destr釋放。

相關問題