2017-04-23 40 views
-3

刪除m_Array時出現了很多問題。當程序正在執行清理部分時,該程序最終會發生段錯誤。我在m_Array中有兩個具有不同數據的A類對象,並且在程序的某個時刻,來自一個對象的數據開始「繞回」到另一個數組中,導致數據不正確。 T表示我的模板化數據類型。C++刪除動態大小的數組對的問題

還有一類B,剛剛創建了兩個A類對象

類聲明中公開宣佈這樣的:

template <typename T> class A 
{ 
public: 
    pair<T, int> *m_Array; // Array of type pair 
    A(int size=1);  // constructor 
    ~A();  // A destructor 

    // ... all other definitions // 

}; 

A類的構造函數定義等被定義:

template <typename T> 
A<T>::A(int size) { 

    // Set array size 
    m_Array = new pair<T, int>[size]; 

    // Initialize values 
    for (int i = 0; i < size; i++) { 
     m_Array[i] = make_pair(-1, -1); 
    } 

    //... other things defined and initialized...// 
} 

在A級的分析器中:

template <typename T> 
A<T>::~A() { 

     delete [] m_Array; // Not working as it should 
} 

重載賦值運算符

template <typename T> 
const A<T>& A<T>::operator=(const A<T>& rhs) { 
    m_AArraySize = rhs.m_AArraySize; 
    m_currASize = rhs.m_currASize; 

    for (int i = 0; i < m_currASize; i++) { 

     m_Array[i].first = rhs.m_Array[i].first; 
     m_Array[i].second = rhs.m_Array[i].second; 
    } 

    _ptr = rhs._ptr; 

    return *this; 
} 

複製構造

template <typename T> 
A<T>::A(const A<T>& other) { 
    m_AArraySize = other.m_AArraySize; 
    m_AHeapSize = other.m_AHeapSize; 

    for (int i = 0; i < m_currASize; i++) { 
     m_Array[i].first = other.m_Array[i].first; 
     m_Array[i].second = other.m_Array[i].second; 
    } 

    _ptr = other._ptr; 
} 

B類聲明

template <typename T> class B{ 
public: 
    //B constructor 
    B(int size); 


    int m_currBSize;  // spots used in array 
    int m_BSize;   // size of array 

    A <T> oneAHolder; 
    A <T> twoAHolder; 

}; 

B類構造函數

template <typename T> 
b<T>::b(int size){ 
    A<T>(size); 

    m_BArraySize = size; 
    m_currBSize = 1; 

    // Create two A objects 
    A<T> oneA(size); 
    A<T> twoA(size); 

    // oneA and twoA go out of scope 
    oneAHolder = oneA; 
    twoAHolder = twoA; 
} 

在我的主要功能正在做的一切都是我創造了一個B類對象,並使用它的插入功能將數據插入到它的兩個一個對象。

我試圖從陣列中刪除數據,並停止數據溢出到另一個陣列的幾種不同的方法,但都無濟於事。

我感謝任何幫助!

PS:請沒有 「只是使用std :: vector的」

編輯:添加更多的我的代碼

+2

你是什麼意思的「環繞」?這聽起來更像是一個雙重的免費問題。你正在使用copy ctor還是將一個對象A分配給另一個'A newA = anotherA'?看看這個https://stackoverflow.com/questions/7823845/disable-compiler-generated-copy-assignment-operator或者實現這些功能。 –

+1

向我們展示你的'main'程序。用你發佈的內容,程序可以很容易地用兩行代碼破解。 – PaulMcKenzie

+1

「只需使用std :: unique_ptr」 –

回答

1

給你發佈的代碼,你的賦值運算符有兩個問題:

1)賦值操作符會泄漏內存,因爲您無法取消分配舊內存。

2)除非this->m_Array已經足夠大,你是在for循環,因爲this->m_Array重寫內存比rhs.m_Array較小的緩衝區。

而不是所有這個錯誤的代碼,你可以簡單地使用copy/swap idiom

#include <algorithm> 
//... 
template <typename T> 
A<T>& A<T>::operator=(const A<T>& rhs) 
{ 
    A<T> temp(rhs); 
    std::swap(temp.m_AArraySize, m_AArraySize); 
    std::swap(temp.m_currASize, m_currASize); 
    std::swap(temp.m_Array, m_Array); 
    std::swap(temp._ptr, _ptr); 
    return *this; 
} 

這工作,如果你寫了一個正確拷貝構造函數不間接調用賦值運算符,以及正確析構函數。如果這些功能中的任何一個出現故障,上述方法將不起作用。

假設複製構造函數和析構函數沒有錯誤,以這種方式編寫賦值操作符避免了從複製構造函數重寫代碼,因爲您只是重用它。

如果A中有更多的成員變量沒有在您的文章中指定,它們也都需要進行交換。

總之,這基本上只是複製了rhs並將this的所有舊數據與temp進行了交換。然後temp在最後用舊數據消失。