2016-11-15 64 views
0

所以這裏是我的一個簡單動態數組的工作代碼。這必須是一個非常入門級的數據結構實現一個示例代碼:Debug Assertion在動態數組上失敗

#include <iostream> 
using namespace std; 

class AdvancedArray { 
public: 
    AdvancedArray(); 
    ~AdvancedArray(); 
    int get_size() const; // get the number of elements stored 
    double& at(int idx) const; // access the element at idx 
    void push_back(double d); // adds a new element 
    void remove(int idx); // remove the element at idx 
    void clear(); // delete all the data stored 
    void print() const; 

private: 
    double* elements; 
    int size; 
}; 

int main() 
{ 
    AdvancedArray* arr = new AdvancedArray(); 
    cout << "The Array Size is: " << arr->get_size() << endl; 
    cout << "Pusing Values: 1.2, 2.1, 3.3, 4.5 in the Array. " << endl; 
    arr->push_back(1.2); 
    arr->push_back(2.1); 
    arr->push_back(3.3); 
    arr->push_back(4.5); 
    arr->print(); 
    cout << "The Array Size is: " << arr->get_size() << endl; 
    cout << "The Element at Index 2 is: " << arr->at(2) << endl; 
    cout << "Deleting Values: 2.1 from the Array. " << endl; 
    arr->remove(1); 
    cout << "The Array Size is: " << arr->get_size() << endl; 
    arr->print(); 
    cout << "Clearing the Array: " << endl; 
    arr->clear(); 
    cout << "The Array Size is: " << arr->get_size() << endl; 
    arr->clear(); 
    return 0; 
} 

AdvancedArray::AdvancedArray() 
{ 
    size = -1; 
    elements = new double[100]; //Maximum Size of the Array 
} 

AdvancedArray::~AdvancedArray() 
{ 
    delete[] elements; 
} 

int AdvancedArray::get_size() const 
{ 
    if(size < 0) 
    { 
     return 0; 
    } 
    return size; 
} 

double & AdvancedArray::at(int idx) const 
{ 
    if (idx < 100 && idx >= 0 && size > 0) { 
     return elements[idx]; 
    } 
    cout << "Index Out of Bounds." << endl; 
} 

void AdvancedArray::push_back(double d) 
{ 
    if (size >= 100) 
    { 
     cout << "Overflow Condition. No More Space!" << endl; 
    } 
    else 
    { 
     elements[++size] = d; 
     cout << "Element Pushed In Stack Successfully!" << endl; 
    } 
} 

void AdvancedArray::remove(int idx) 
{ 
    if (size >= 100 || size < 0) 
    { 
     cout << "No Such Element Exists!" << endl; 
    } 
    else 
    { 
     for(int i = idx; i <size; i++) 
     { 
      elements[idx] = elements[idx + 1]; 
     } 
     size--; 
     cout << "Element Deleted In Stack Successfully!" << endl; 
    } 
} 

void AdvancedArray::clear() 
{ 
    delete[] elements; 
    size = -1; 
} 

void AdvancedArray::print() const 
{ 
    cout << "[ "; 
    for(int i = 0; i <= size; i++) 
    { 
     cout << elements[i] << " "; 
    } 
    cout << "]" << endl; 
} 

所以每次我嘗試運行這段時間我有2個問題:

enter image description here

enter image description here

我的代碼有什麼問題?爲什麼堆被損壞(我搜索了錯誤代碼,這就是所有人都說的)?我的代碼是否執行一些主要的訪問違規?我正在使用VS2015。

+0

想想當你兩次調用clear時會發生什麼。 –

+1

...或清除並調用析構函數。 – Mat

+0

@RetiredNinja刪除了一個清除,仍然錯誤仍然存​​在! –

回答

5

你做delete [] elements 倍之間沒有設置elementsnullptr。這導致第二次(和第三次)未定義行爲

+0

刪除一個清除,仍然錯誤仍然存​​在! –

+0

@ Jeet.Deir這是因爲你做*三次*。剛剛更新了我的問題,不要刪除'clear'調用,將'elements'設置爲'nullptr',你不必擔心,允許在空指針上執行'delete []'' –

+0

更簡單從清除中刪除delete []''''''''''''''''''''''''''''''''''''''''''只能在構造函數中進行分配如果用戶清除數組並試圖添加一個值,它也會出現段錯誤 – Ari0nhh

2

size == 99,下面這段代碼試圖訪問elements[100]

if (size >= 100) 
{ 
    cout << "Overflow Condition. No More Space!" << endl; 
} 
else 
{ 
    elements[++size] = d; 
    cout << "Element Pushed In Stack Successfully!" << endl; 
} 

您需要更改++sizesize++

+0

我仍然得到堆腐敗檢測:( –

+0

我看到清除看到叫過兩次 – P0W

+0

刪除一個清除,仍然錯誤仍然存​​在! –

相關問題